Skip to content

Commit

Permalink
Updated to DLL build
Browse files Browse the repository at this point in the history
  • Loading branch information
skynet2 committed Jun 2, 2015
1 parent 3fbc06d commit 7d2994e
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 68 deletions.
126 changes: 73 additions & 53 deletions pwAPI/Readers/ElementReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ElementReader
{
public Dictionary<string, Item[]> Items { get; set; }
private readonly BinaryReader _br;
private readonly List<ConfigList> _confList;
private readonly ConfigLists _confList;
public HashSet<int> ExistingId;
private string _path;
// SAVERS
Expand All @@ -21,32 +21,38 @@ public class ElementReader
public ElementReader(string configPath, string elementPath)
{
_path = elementPath;
_confList = ConfigList.ParseList(configPath);
_confList = new ConfigLists();
_confList.ParseList(configPath);
Items = new Dictionary<string, Item[]>();
_br = new BinaryReader(File.OpenRead(elementPath));
_somevals = new Dictionary<byte, List<byte[]>>();
var bb = new List<byte[]> { _br.ReadBytes(8) };
_somevals.Add(0, bb);
var bb = new List<byte[]> { _br.ReadBytes(4) };
_somevals.Add(255, bb);

for (var i = 0; i < _confList.Count; i++)
for (byte i = 0; i < _confList.Lists.Count; i++)
{
switch (i)
if(i != _confList.NpcTalk)
{
case 20:
_somevals.Add(Convert.ToByte(i), Offset20());
Items.Add(_confList[i].Name, List(i));
break;
case 58:
_somevals.Add(Convert.ToByte(i), List58());
break;
case 100:
_somevals.Add(Convert.ToByte(i), Offset100());
Items.Add(_confList[i].Name, List(i));
break;
default:
Items.Add(_confList[i].Name, List(i));
break;
if(_confList.Lists[i].Skip != "0" && _confList.Lists[i].Skip != "AUTO")
_somevals[i] = new List<byte[]>{_br.ReadBytes(int.Parse(_confList.Lists[i].Skip))};
else if(_confList.Lists[i].Skip == "AUTO")
{
_somevals[i] = new List<byte[]> { _br.ReadBytes(4) };
var count = _br.ReadInt32();
_somevals[i].Add(IntToByte(count));
_somevals[i].Add(_br.ReadBytes(count));
count = _br.ReadInt32();
while(count <= 0 || count > 10000)
{
_somevals[i].Add(IntToByte(count));
count = _br.ReadInt32();
}
_br.BaseStream.Position -= 4;
}
Items.Add(_confList.Lists[i].Name, List(i));
}
else
_somevals.Add(Convert.ToByte(i), List58());
}
Console.WriteLine(Process.GetCurrentProcess().WorkingSet64);
Console.WriteLine((GC.GetTotalMemory(true) / 1024f) / 1024f);
Expand Down Expand Up @@ -92,19 +98,10 @@ private Item[] List(int i)
var count = _br.ReadInt32();
var items = new Item[count];
for (var z = 0; z < count; z++)
items[z] = Item.ParseItem(_confList[i], _br);
items[z] = Item.ParseItem(_confList.Lists[i], _br);
return items;
}

private List<byte[]> Offset100()
{
var off100 = new List<byte[]> { _br.ReadBytes(4) };
var count = _br.ReadInt32();
off100.Add(IntToByte(count));
off100.Add(_br.ReadBytes(count));
return off100;
}

private List<byte[]> List58()
{
var count = _br.ReadInt32();
Expand Down Expand Up @@ -151,40 +148,52 @@ public void AddItem(string key, Item newItem)
Items[key] = arr;
}

public Item GetLastInList(int list)
{
Item[] ls = GetListById(list);
return ls.Last();
}
public Item FindInList(int listID, int id)
{
foreach (var it in GetListById(listID).Where(it => it.GetByKey("ID") == id))
return it;
return null;
}
public int AddItem(int listID, Item newItem, bool print = false)

public int AddItem(int listID, Item newItem, bool checkID = true)
{
if (ExistingId == null)
ElementUtils.GetExsistingIDs(this);
var key = GetListKey(listID);
if (ExistingId.Contains(Convert.ToInt32(newItem.GetByKey("ID"))))
newItem.SetByKey("ID", GetFreeId());
if (print) PrintInfo(newItem);
AddItem(key, newItem);
return newItem.GetByKey("ID");
if (checkID)
{
if (ExistingId.Contains(Convert.ToInt32(newItem.GetByKey("ID"))))
newItem.SetByKey("ID", GetFreeId());
}
// if (print) PrintInfo(newItem);
var it = ElementUtils.AdvancedCopy(GetFirstInList(listID), newItem);
AddItem(key, it);
return it.GetByKey("ID");
}

public void RemoveItem(int list, Item it)
{
Item removeItem;
var arr = new Item[GetListById(list).Length-1];
int i = 0;
bool deleted = false;
foreach (var items in GetListById(list))
{
if (items.GetByKey("ID") == it.GetByKey("ID"))
if (it == null || (items.GetByKey("ID") == it.GetByKey("ID")) && !deleted)
{
deleted = true;
continue;
}
arr[i++] = items;
}
Items[GetListKey(list)] = arr;
}
public string GetIcon(int recepie)
public string GetIcon70(int recepie)
{
Item it = FindInList(70, recepie);
if (it == null)
Expand All @@ -201,7 +210,23 @@ public string GetIcon(int recepie)
return kk;
}
return null;
}

public string GetIcon(int item)
{
if (item == 0)
return "";
for(int i = 3 ; i <= Items.Count; i++)
// foreach (var page in Items)
{
var page = Items.ElementAt(i);
foreach (Item it in page.Value)
{
if (Convert.ToInt32(it.GetByKey("ID")) == item)
return it.GetByKey("file_icon");
}
}
return "";
}
private static void PrintInfo(Item i)
{
Expand Down Expand Up @@ -231,31 +256,26 @@ public void Save(String newPath = null, bool zip = false)
{
if (newPath == null) newPath = _path;
var bw = new BinaryWriter(File.OpenWrite(newPath));
WriteList(bw, _somevals[0]);
var l58 = _confList[58];
_confList.Remove(l58);
for (int i = 0; i < Items.Keys.Count; i++)
WriteList(bw, _somevals[255]);
for(byte i = 0; i < _confList.Lists.Count; i++)
{
if (i == 20)
{
WriteList(bw, _somevals[20]);
}
if (i == 58)
if(i < 255 && i != _confList.NpcTalk && _somevals.ContainsKey(i))
{
WriteList(bw, _somevals[58]);
WriteList(bw, _somevals[i]);
}
if (i == 99)
if(i == _confList.NpcTalk)
{
WriteList(bw, _somevals[100]);
WriteList(bw, _somevals[_confList.NpcTalk]);
continue;
}
var keys = Items.Keys;
var currentKey = keys.ElementAt(i);
var currentKey = keys.ElementAt(i > _confList.NpcTalk ? i - 1: i);
bw.Write(Items[currentKey].Length);
foreach (var item in Items[currentKey])
item.Save(bw, _confList[i]);
if(item != null)
item.Save(bw, _confList.Lists[i]);

}
_confList.Insert(58, l58);
bw.Close();

}
Expand Down
92 changes: 92 additions & 0 deletions pwAPI/Readers/RegionReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using pwApi.StructuresRegion;

namespace pwApi.Readers
{
public class RegionReader
{
public RegionStruct Region { get; set; }
public RegionReader(string path)
{
try
{
Region = new RegionStruct
{
Regions = new List<Region>(),
Transp = new List<Transport>()
};
using (var br = new BinaryReader(File.OpenRead(path)))
{
Region.Version = br.ReadInt32();
Region.iNumRegion = br.ReadInt32();
if (Region.Version > 1)
Region.iNumTrans = br.ReadInt32();
if (Region.Version > 3)
Region.TimeStamp = br.ReadInt32();

for (int i = 0; i < Region.iNumTrans + Region.iNumRegion; i++)
{
int type = br.ReadInt32();
if (type == 0)
Region.Regions.Add(new Region(br, i));
else Region.Transp.Add(new Transport(br, Region.Version,i));
}
}
}
catch (Exception e)
{
throw e;
}

}

public void SaveAsText(string path)
{
using (var wr = new StreamWriter(path, false, Encoding.GetEncoding(1200)))
{
wr.WriteLine("version 5");
wr.WriteLine("1312962951");
wr.WriteLine();
foreach (var region in Region.Regions)
{
region.SaveAsText(wr);
wr.WriteLine();
}
foreach (var trans in Region.Transp)
{
trans.SaveAsText(wr);
wr.WriteLine();
}
}

}
public void Save(string path)
{
using (var bw = new BinaryWriter(File.OpenWrite(path)))
{
bw.Write(Region.Version);
bw.Write(Region.Regions.Count);
if(Region.Version > 1)
bw.Write(Region.Transp.Count);
if(Region.Version > 3)
bw.Write(Region.TimeStamp);
foreach (var reg in Region.Regions)
{
bw.Write(0);
reg.Save(bw);
}
foreach (var tra in Region.Transp)
{
bw.Write(1);
tra.Save(bw,Region.Version);
}
}
}
}
}
33 changes: 23 additions & 10 deletions pwAPI/StructuresElement/ConfigList.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;

namespace pwApi.StructuresElement
{
public class ConfigList
{
public string Name;
public string Skip;
public Type[] Types;
public ConfigList(String name, String values, String types)

public ConfigList(string name, string skip, string values, string types)
{
Name = name;
Skip = skip;
var vals = values.Split(';');
var tty = types.Split(';');
Types = new Type[vals.Length];

for (int i = 0; i < vals.Length; i++)
for(var i = 0; i < vals.Length; i++)
Types[i] = new Type(vals[i], tty[i]);
}
public static List<ConfigList> ParseList(String path)
}

public class ConfigLists
{
public byte NpcTalk;
public List<ConfigList> Lists;

public void ParseList(string path)
{
List<ConfigList> lists = new List<ConfigList>();
Lists = new List<ConfigList>();
var args = File.ReadAllLines(path);
for (int i = 2; i <= args.Length - 2; i += 5)
var count = int.Parse(args[0]);
NpcTalk = byte.Parse(args[1]);
var line = 2;
for(var i = 0; i < count; i++)
{
lists.Add(new ConfigList(args[i + 1], args[i + 3], args[i + 4]));
while(args[line] == "")
line++;
Lists.Add(new ConfigList(args[line], args[line + 1], args[line + 2], args[line + 3]));
line += 4;
}
return lists;
}
}
}

0 comments on commit 7d2994e

Please sign in to comment.