Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 74349c1

Browse files
committed
Zip support
1 parent da22f53 commit 74349c1

File tree

10 files changed

+140
-128
lines changed

10 files changed

+140
-128
lines changed

Content/LeagueSandbox-Default

GameServerCore/Extensions.cs

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4-
using System.IO;
5-
using System.Linq;
64
using System.Numerics;
75
using System.Text;
86
using GameServerCore.Enums;
@@ -95,69 +93,6 @@ public static double RadianToDegree(double angle)
9593
{
9694
return angle * (180.0 / Math.PI);
9795
}
98-
99-
public static IEnumerable<string> GetAllFilesInDirectory(string directory, Predicate<string> pred)
100-
{
101-
if (pred == null)
102-
{
103-
pred = _ => true;
104-
}
105-
106-
var list = Directory.GetFiles(directory).Where(x => pred.Invoke(x)).ToList();
107-
108-
foreach (var subdirectory in Directory.GetDirectories(directory))
109-
{
110-
list.AddRange(GetAllFilesInDirectory(subdirectory, pred));
111-
}
112-
113-
return list;
114-
}
115-
}
116-
117-
public class PairList<TKey, TValue> : List<Pair<TKey, TValue>>
118-
{
119-
public void Add(TKey key, TValue value)
120-
{
121-
Add(new Pair<TKey, TValue>(key, value));
122-
}
123-
public bool ContainsKey(TKey key)
124-
{
125-
foreach (var v in this)
126-
{
127-
if (v.Item1.Equals(key))
128-
{
129-
return true;
130-
}
131-
}
132-
133-
return false;
134-
}
135-
136-
public TValue this[TKey key]
137-
{
138-
get
139-
{
140-
foreach (var v in this)
141-
{
142-
if (v.Item1.Equals(key))
143-
{
144-
return v.Item2;
145-
}
146-
}
147-
148-
return default(TValue);
149-
}
150-
set
151-
{
152-
foreach (var v in this)
153-
{
154-
if (v.Item1.Equals(key))
155-
{
156-
v.Item2 = value;
157-
}
158-
}
159-
}
160-
}
16196
}
16297

16398
public static class CustomConvert

GameServerLib/Config.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Numerics;
5+
using System.Text;
56
using LeagueSandbox.GameServer.Content;
67
using Newtonsoft.Json.Linq;
78

@@ -76,7 +77,7 @@ private void LoadConfig(Game game, string json)
7677
// Read spawns info
7778
ContentManager = ContentManager.LoadGameMode(game, GameConfig.GameMode, ContentPath);
7879
var mapPath = ContentManager.GetMapConfigPath(GameConfig.Map);
79-
var mapData = JObject.Parse(File.ReadAllText(mapPath));
80+
var mapData = JObject.Parse(Encoding.UTF8.GetString(ContentManager.Content[mapPath]));
8081
var spawns = mapData.SelectToken("spawns");
8182

8283
// Load items

GameServerLib/Content/CharData.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
22
using System.IO;
33
using GameServerCore.Enums;
4+
using IniParser;
45
using LeagueSandbox.GameServer.Logging;
56
using log4net;
67
using LeagueSandbox.GameServer.Exceptions;
7-
using Newtonsoft.Json;
88

99
namespace LeagueSandbox.GameServer.Content
1010
{
@@ -79,12 +79,17 @@ public void Load(string name)
7979
return;
8080
}
8181

82-
var file = new ContentFile();
82+
ContentFile file;
8383
try
8484
{
8585
var path = _game.Config.ContentManager.GetUnitStatPath(name);
86+
var iniParser = new FileIniDataParser();
8687
_logger.Debug($"Loading {name}'s Stats from path: {Path.GetFullPath(path)}!");
87-
file = _game.Config.ContentManager.Content[path];
88+
using (var stream = new StreamReader(new MemoryStream(_game.Config.ContentManager.Content[path])))
89+
{
90+
var iniData = iniParser.ReadData(stream);
91+
file = new ContentFile(ContentManager.ParseIniFile(iniData));
92+
}
8893
}
8994
catch (ContentNotFoundException notfound)
9095
{

GameServerLib/Content/ContentManager.cs

Lines changed: 91 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.IO;
3+
using System.IO.Compression;
44
using System.Linq;
5-
using IniParser;
65
using IniParser.Model;
76
using LeagueSandbox.GameServer.Logging;
87
using log4net;
98
using LeagueSandbox.GameServer.Exceptions;
10-
using Newtonsoft.Json.Linq;
119

1210
namespace LeagueSandbox.GameServer.Content
1311
{
@@ -20,15 +18,12 @@ public class ContentManager
2018
private Dictionary<string, CharData> _charData = new Dictionary<string, CharData>();
2119
private Dictionary<string, NavGrid> _navGrids = new Dictionary<string, NavGrid>();
2220

23-
public Dictionary<string, ContentFile> Content = new Dictionary<string, ContentFile>();
24-
public List<string> CSharpScriptFiles = new List<string>();
21+
public Dictionary<string, byte[]> Content = new Dictionary<string, byte[]>();
2522

26-
private string _contentPath;
2723
public string GameModeName { get; }
2824

29-
private ContentManager(Game game, string gameModeName, string contentPath)
25+
private ContentManager(Game game, string gameModeName)
3026
{
31-
_contentPath = contentPath;
3227
_game = game;
3328
_logger = LoggerProvider.GetLogger();
3429

@@ -37,18 +32,26 @@ private ContentManager(Game game, string gameModeName, string contentPath)
3732

3833
public string GetMapConfigPath(int mapId)
3934
{
40-
var path = Path.Combine(_contentPath, GameModeName, "LEVELS", $"Map{mapId}", $"Map{mapId}.json");
41-
if (!File.Exists(path))
35+
var possibilities = new[]
4236
{
43-
throw new ContentNotFoundException($"Map configuration for Map {mapId} was not found in the content.");
37+
$"LEVELS/Map{mapId}/Map{mapId}.json",
38+
$"LEVELS/map{mapId}/Map{mapId}.json"
39+
};
40+
41+
foreach (var path in possibilities)
42+
{
43+
if (Content.ContainsKey(path))
44+
{
45+
return path;
46+
}
4447
}
4548

46-
return path;
49+
throw new ContentNotFoundException($"Map configuration for Map {mapId} was not found in the content.");
4750
}
4851

4952
public string GetUnitStatPath(string model)
5053
{
51-
var path = Path.Combine("DATA", "Characters", model, $"{model}.ini");
54+
var path = $"DATA/Characters/{model}/{model}.ini";
5255
if (!Content.ContainsKey(path))
5356
{
5457
throw new ContentNotFoundException($"Stat file for {model} was not found.");
@@ -61,9 +64,9 @@ public string GetSpellDataPath(string model, string spellName)
6164
{
6265
var possibilities = new[]
6366
{
64-
Path.Combine("DATA", "Characters", model, "Spells", $"{spellName}.ini"),
65-
Path.Combine("DATA", "Shared", "Spells", $"{spellName}.ini"),
66-
Path.Combine("DATA", "Spells", $"{spellName}.ini")
67+
$"DATA/Characters/{model}/Spells/{spellName}.ini",
68+
$"DATA/Shared/Spells/{spellName}.ini",
69+
$"DATA/Spells/{spellName}.ini"
6770
};
6871

6972
foreach (var path in possibilities)
@@ -101,27 +104,84 @@ public CharData GetCharData(string charName)
101104
return _charData[charName];
102105
}
103106

104-
public NavGrid GetNavGrid(string navGridPath)
107+
public NavGrid GetNavGrid(int mapId)
105108
{
106-
if (_navGrids.ContainsKey(navGridPath))
109+
var possibilities = new[]
110+
{
111+
$"LEVELS/Map{mapId}/AIPath.aimesh_ngrid",
112+
$"LEVELS/map{mapId}/AIPath.aimesh_ngrid"
113+
};
114+
foreach (var path in possibilities)
107115
{
108-
return _navGrids[navGridPath];
116+
if (_navGrids.ContainsKey(path))
117+
{
118+
return _navGrids[path];
119+
}
109120
}
110121

111-
throw new ContentNotFoundException($"NavGrid with path {navGridPath} was not loaded.");
122+
throw new ContentNotFoundException($"NavGrid for map {mapId} was not loaded.");
112123
}
113124

114125
public static ContentManager LoadGameMode(Game game, string gameModeName, string contentPath)
115126
{
116-
var contentManager = new ContentManager(game, gameModeName, contentPath);
117-
var iniParser = new FileIniDataParser();
127+
var contentManager = new ContentManager(game, gameModeName);
128+
129+
var zipPath = Path.Combine(contentPath, gameModeName, gameModeName + ".zip");
130+
131+
// If zip exists
132+
if (File.Exists(zipPath))
133+
{
134+
// Read zip file data
135+
using (var file = File.OpenRead(zipPath))
136+
{
137+
// Read archive data
138+
using (var zip = new ZipArchive(file, ZipArchiveMode.Read))
139+
{
140+
// For every entry in the zip
141+
foreach (var entry in zip.Entries)
142+
{
143+
// Uncompress the entry and read it
144+
using (var entryData = new BinaryReader(entry.Open()))
145+
{
146+
if (entry.FullName.EndsWith(".ini") || entry.FullName.EndsWith(".json"))
147+
{
148+
contentManager.Content[entry.FullName] = entryData.ReadBytes((int)entry.Length);
149+
}
150+
else if (entry.FullName.EndsWith(".aimesh_ngrid"))
151+
{
152+
contentManager._navGrids[entry.FullName] =
153+
NavGridReader.ReadBinary(entryData.ReadBytes((int)entry.Length));
154+
155+
contentManager.Content[entry.FullName] = entryData.ReadBytes((int)entry.Length);
156+
}
157+
else if (entry.FullName.EndsWith(".cs"))
158+
{
159+
if (entry.FullName.StartsWith("bin") || entry.FullName.StartsWith("obj"))
160+
{
161+
continue;
162+
}
163+
164+
contentManager.Content[entry.FullName] = entryData.ReadBytes((int)entry.Length);
165+
}
166+
else
167+
{
168+
continue;
169+
}
170+
171+
contentManager._logger.Debug($"Mapped content from zip [{entry.FullName}]");
172+
}
173+
}
174+
}
175+
}
176+
}
177+
118178
foreach (var file in Directory.GetFiles(contentPath, "*.*", SearchOption.AllDirectories))
119179
{
120-
var relativePath = file.Replace(contentPath, "").Replace(gameModeName, "").Substring(2);
121-
if (file.EndsWith(".ini"))
180+
var relativePath = file.Replace(contentPath, "").Replace(gameModeName, "").Substring(2)
181+
.Replace('\\', '/');
182+
if (file.EndsWith(".ini") || file.EndsWith(".json"))
122183
{
123-
var ini = iniParser.ReadFile(file);
124-
contentManager.Content[relativePath] = new ContentFile(ParseIniFile(ini));
184+
contentManager.Content[relativePath] = File.ReadAllBytes(file);
125185
}
126186
else if (file.EndsWith(".cs"))
127187
{
@@ -130,10 +190,11 @@ public static ContentManager LoadGameMode(Game game, string gameModeName, string
130190
continue;
131191
}
132192

133-
contentManager.CSharpScriptFiles.Add(file);
193+
contentManager.Content[relativePath] = File.ReadAllBytes(file);
134194
}
135195
else if (file.EndsWith(".aimesh_ngrid"))
136196
{
197+
contentManager.Content[relativePath] = File.ReadAllBytes(file);
137198
contentManager._navGrids[relativePath] = NavGridReader.ReadBinary(file);
138199
}
139200
else
@@ -156,6 +217,7 @@ public static Dictionary<string, Dictionary<string, string>> ParseIniFile(IniDat
156217
{
157218
ret[section.SectionName] = new Dictionary<string, string>();
158219
}
220+
159221
foreach (var field in section.Keys)
160222
{
161223
ret[section.SectionName][field.KeyName] = field.Value;
@@ -189,4 +251,4 @@ private static bool ValidatePackageName(string packageName)
189251
return true;
190252
}
191253
}
192-
}
254+
}

GameServerLib/Content/ItemManager.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Linq;
44
using GameServerCore.Domain;
5+
using IniParser;
56
using LeagueSandbox.GameServer.GameObjects.Stats;
67
using LeagueSandbox.GameServer.Items;
78

@@ -43,17 +44,25 @@ public void ResetItems()
4344

4445
public void LoadItems(ContentManager contentManager)
4546
{
47+
var iniParser = new FileIniDataParser();
4648
foreach (var content in contentManager.Content)
4749
{
48-
if (!content.Key.ToLowerInvariant().StartsWith(Path.Combine("data", "items")))
50+
if (!content.Key.StartsWith("DATA/Items"))
4951
{
5052
continue;
5153
}
5254

53-
var split = content.Key.Replace('\\', '/').Split('/');
55+
var split = content.Key.Split('/');
5456
var itemIdStr = split.Last().Replace(".ini", "");
5557

56-
var itemData = ItemData.Load(this, content.Value, int.Parse(itemIdStr));
58+
ContentFile contentFile;
59+
using (var stream = new StreamReader(new MemoryStream(content.Value)))
60+
{
61+
var iniData = iniParser.ReadData(stream);
62+
contentFile = new ContentFile(ContentManager.ParseIniFile(iniData));
63+
}
64+
65+
var itemData = ItemData.Load(this, contentFile, int.Parse(itemIdStr));
5766
_itemData.Add(itemData.ItemId, itemData);
5867
}
5968
}

0 commit comments

Comments
 (0)