Added download for maps. Added "MapLoader.cs". Small changes. Added new functions for Javascript

This commit is contained in:
EntenKoeniq
2022-04-11 13:07:46 +02:00
parent 67c4d9a694
commit cbc02b48be
8 changed files with 197 additions and 15 deletions

View File

@ -95,6 +95,7 @@
<Compile Include="JavascriptHook.cs" />
<Compile Include="Logger.cs" />
<Compile Include="Main.cs" />
<Compile Include="MapLoader.cs" />
<Compile Include="Menus\MenusMain.cs" />
<Compile Include="Menus\Sub\Servers.cs" />
<Compile Include="Menus\Sub\Settings.cs" />

View File

@ -31,6 +31,17 @@ namespace CoopClient
}
return;
}
else if (!new string[] { ".js", ".xml" }.Any(x => x == Path.GetExtension(name)))
{
lock (_filesFinished)
{
_filesFinished.Add(id);
}
GTA.UI.Notification.Show($"The download of a file from the server was blocked! [{name}]", true);
Logger.Write($"The download of a file from the server was blocked! [{name}]", Logger.LogLevel.Server);
return;
}
lock (_downloadFiles)
{

View File

@ -251,12 +251,6 @@ namespace CoopClient.Entities.Player
Character.CanSufferCriticalHits = false;
// Add a new blip for the ped
Character.AddBlip();
Character.AttachedBlip.Color = BlipColor.White;
Character.AttachedBlip.Scale = 0.8f;
Character.AttachedBlip.Name = Username;
Function.Call(Hash.SET_PED_CAN_EVASIVE_DIVE, Character.Handle, false);
Function.Call(Hash.SET_PED_DROPS_WEAPONS_WHEN_DEAD, Character.Handle, false);
Function.Call(Hash.SET_PED_CAN_BE_TARGETTED, Character.Handle, true);
@ -267,6 +261,12 @@ namespace CoopClient.Entities.Player
SetClothes();
// Add a new blip for the ped
Character.AddBlip();
Character.AttachedBlip.Color = BlipColor.White;
Character.AttachedBlip.Scale = 0.8f;
Character.AttachedBlip.Name = Username;
return true;
}

View File

@ -423,6 +423,21 @@ namespace CoopClient
return Game.IsControlPressed((Control)control);
}
public bool IsMapLoaded()
{
return MapLoader.AnyMapLoaded();
}
public void LoadMap(string name)
{
MapLoader.LoadMap(name);
}
public void DeleteMap()
{
MapLoader.DeleteMap();
}
public void TriggerServerEvent(string eventName, params object[] args)
{
// TODO

View File

@ -110,11 +110,6 @@ namespace CoopClient
MainNetworking.ReceiveMessages();
if (!JavascriptHook.JavascriptLoaded && DownloadManager.DownloadComplete)
{
JavascriptHook.LoadAll();
}
if (_isGoingToCar && Game.Player.Character.IsInVehicle())
{
_isGoingToCar = false;
@ -125,6 +120,12 @@ namespace CoopClient
return;
}
if (!JavascriptHook.JavascriptLoaded && DownloadManager.DownloadComplete)
{
MapLoader.LoadAll();
JavascriptHook.LoadAll();
}
#if DEBUG
if (MainNetworking.ShowNetworkInfo)
{

145
Client/MapLoader.cs Normal file
View File

@ -0,0 +1,145 @@
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using System.Collections.Generic;
using GTA;
using GTA.Math;
using GTA.Native;
using System.Xml;
namespace CoopClient
{
public class Map
{
[XmlArray("Props")]
[XmlArrayItem("Prop")]
public Props[] Props { get; set; }
}
public class Props
{
public Vector3 Position { get; set; }
public Vector3 Rotation { get; set; }
public int Hash { get; set; }
public bool Dynamic { get; set; }
public int Texture { get; set; }
}
internal static class MapLoader
{
// string = file name
private static readonly Dictionary<string, Map> _maps = new Dictionary<string, Map>();
private static readonly List<int> _createdObjects = new List<int>();
public static void LoadAll()
{
string downloadFolder = $"scripts\\resources\\{Main.MainSettings.LastServerAddress.Replace(":", ".")}";
if (!Directory.Exists(downloadFolder))
{
Directory.CreateDirectory(downloadFolder);
}
string[] files = Directory.GetFiles(downloadFolder, "*.xml");
lock (_maps)
{
for (int i = 0; i < files.Length; i++)
{
string filePath = files[i];
XmlSerializer serializer = new XmlSerializer(typeof(Map));
Map map;
using (var stream = new FileStream(filePath, FileMode.Open))
{
map = (Map)serializer.Deserialize(stream);
}
GTA.UI.Notification.Show($"{map.Props.Count()}");
string fileName = Path.GetFileName(filePath);
_maps.Add(fileName, map);
GTA.UI.Notification.Show($"test: {_maps["ATV.xml"].Props.Count()}");
}
//GTA.UI.Notification.Show($"{_maps["ATV.xml"].Objects[0].Position.X}");
}
}
public static void LoadMap(string name)
{
lock (_maps) lock (_createdObjects)
{
if (!_maps.ContainsKey(name) || _createdObjects.Count != 0)
{
GTA.UI.Notification.Show($"The map with the name \"{name}\" couldn't be loaded!");
return;
}
Map map = _maps[name];
for (int i = 0; i < map.Props.Count(); i++)
{
Props prop = map.Props[i];
Model model = prop.Hash.ModelRequest();
if (model == null)
{
Logger.Write($"Model for object \"{model.Hash}\" couldn't be loaded!", Logger.LogLevel.Server);
continue;
}
int handle = Function.Call<int>(Hash.CREATE_OBJECT, model.Hash, prop.Position.X, prop.Position.Y, prop.Position.Z, 1, 1, prop.Dynamic);
if (handle == 0)
{
Logger.Write($"Object \"{model.Hash}\" couldn't be created!", Logger.LogLevel.Server);
continue;
}
model.MarkAsNoLongerNeeded();
_createdObjects.Add(handle);
if (prop.Texture > 0 && prop.Texture < 16)
{
Function.Call(Hash._SET_OBJECT_TEXTURE_VARIATION, handle, prop.Texture);
}
Logger.Write($"Object [{model.Hash}] created at {prop.Position.X}, {prop.Position.Y}, {prop.Position.Z}", Logger.LogLevel.Server);
}
}
}
public static bool AnyMapLoaded()
{
return _createdObjects.Any();
}
public static void DeleteMap()
{
lock (_createdObjects)
{
foreach (int handle in _createdObjects)
{
unsafe
{
int tmpHandle = handle;
Function.Call(Hash.DELETE_OBJECT, &tmpHandle);
}
}
_createdObjects.Clear();
}
}
public static void DeleteAll()
{
DeleteMap();
lock (_maps)
{
_maps.Clear();
}
}
}
}

View File

@ -159,6 +159,7 @@ namespace CoopClient
GTA.UI.Notification.Show("~r~Disconnected: " + reason);
JavascriptHook.StopAll();
MapLoader.DeleteAll();
Logger.Write($">> Disconnected << reason: {reason}", Logger.LogLevel.Server);
break;

View File

@ -41,9 +41,9 @@ namespace CoopServer
FileInfo fileInfo = new(file);
// ONLY JAVASCRIPT AND JSON FILES!
if (!new string[] { ".js", ".json" }.Any(x => x == fileInfo.Extension))
if (!new string[] { ".js", ".xml" }.Any(x => x == fileInfo.Extension))
{
Logging.Warning("Only files with \"*.js\" and \"*.json\" can be sent!");
Logging.Warning("Only files with \"*.js\" and \"*.xml\" can be sent!");
continue;
}
@ -60,7 +60,14 @@ namespace CoopServer
{
if (!fileCreated && (fileCreated = true))
{
newFile = new() { FileID = fileCount, FileName = fileInfo.Name, FileLength = fileInfo.Length, FileChunks = new() };
newFile = new()
{
FileID = fileCount,
FileName = fileInfo.Name,
FileType = fileInfo.Extension == ".json" ? Packets.DataFileType.Script : Packets.DataFileType.Map,
FileLength = fileInfo.Length,
FileChunks = new()
};
}
newFile.FileChunks.Add(buffer.Take(bytesRead).ToArray());
@ -184,7 +191,7 @@ namespace CoopServer
new Packets.FileTransferRequest()
{
ID = file.FileID,
FileType = (byte)Packets.DataFileType.Script,
FileType = (byte)file.FileType,
FileName = file.FileName,
FileLength = file.FileLength
}.PacketToNetOutGoingMessage(outgoingMessage);
@ -256,6 +263,7 @@ namespace CoopServer
{
public byte FileID { get; set; } = 0;
public string FileName { get; set; } = string.Empty;
public Packets.DataFileType FileType { get; set; } = Packets.DataFileType.Script;
public long FileLength { get; set; } = 0;
public List<byte[]> FileChunks { get; set; } = null;
}