Skip to content

Commit

Permalink
Changing splitDLL/DLLModGenerator INI handling in preparation for mod…
Browse files Browse the repository at this point in the history
… loader changes.
  • Loading branch information
MainMemory committed Mar 7, 2015
1 parent b56e7f7 commit 9e513e6
Show file tree
Hide file tree
Showing 5 changed files with 445 additions and 142 deletions.
1 change: 1 addition & 0 deletions DLLModGenerator/DLLModGenerator.csproj
Expand Up @@ -41,6 +41,7 @@
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="IniData.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
204 changes: 204 additions & 0 deletions DLLModGenerator/IniData.cs
@@ -0,0 +1,204 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Text;
using SA_Tools;

namespace DLLModGenerator
{
public class IniData
{
[IniName("game")]
[DefaultValue(Game.SADX)]
public Game Game { get; set; }
[IniName("modulename")]
public string ModuleName { get; set; }
[IniCollection(IniCollectionMode.IndexOnly)]
public Dictionary<string, FileInfo> Files { get; set; }
}

public enum Game
{
SADX,
SA2B
}

public class FileInfo
{
[IniName("type")]
public string Type { get; set; }
[IniName("length")]
public int Length { get; set; }
[IniName("filename")]
public string Filename { get; set; }
}

public class DllIniData
{
[IniName("name")]
public string Name { get; set; }
[IniAlwaysInclude]
[IniName("game")]
public Game Game { get; set; }
public DictionaryContainer<string> Exports { get; set; }
public DictionaryContainer<FileTypeHash> Files { get; set; }
[IniName("Item")]
[IniCollection(IniCollectionMode.NoSquareBrackets, StartIndex = 1)]
public List<DllItemInfo> Items { get; set; }

public DllIniData()
{
Exports = new DictionaryContainer<string>();
Files = new DictionaryContainer<FileTypeHash>();
Items = new List<DllItemInfo>();
}
}

public class DictionaryContainer<T> : IDictionary<string, T>
{
[IniCollection(IniCollectionMode.IndexOnly)]
public Dictionary<string, T> Items { get; set; }

public DictionaryContainer()
{
Items = new Dictionary<string, T>();
}

public void Add(string key, T value)
{
Items.Add(key, value);
}

public bool ContainsKey(string key)
{
return Items.ContainsKey(key);
}

public ICollection<string> Keys
{
get { return Items.Keys; }
}

public bool Remove(string key)
{
return Items.Remove(key);
}

public bool TryGetValue(string key, out T value)
{
return Items.TryGetValue(key, out value);
}

public ICollection<T> Values
{
get { return Items.Values; }
}

public T this[string key]
{
get
{
return Items[key];
}
set
{
Items[key] = value;
}
}

public void Clear()
{
Items.Clear();
}

public int Count
{
get { return Items.Count; }
}

bool ICollection<KeyValuePair<string, T>>.IsReadOnly
{
get { return false; }
}

public IEnumerator<KeyValuePair<string, T>> GetEnumerator()
{
return Items.GetEnumerator();
}

void ICollection<KeyValuePair<string, T>>.Add(KeyValuePair<string, T> item)
{
throw new NotImplementedException();
}

void ICollection<KeyValuePair<string, T>>.Clear()
{
throw new NotImplementedException();
}

bool ICollection<KeyValuePair<string, T>>.Contains(KeyValuePair<string, T> item)
{
throw new NotImplementedException();
}

void ICollection<KeyValuePair<string, T>>.CopyTo(KeyValuePair<string, T>[] array, int arrayIndex)
{
throw new NotImplementedException();
}

bool ICollection<KeyValuePair<string, T>>.Remove(KeyValuePair<string, T> item)
{
throw new NotImplementedException();
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

[System.ComponentModel.TypeConverter(typeof(StringConverter<FileTypeHash>))]
public class FileTypeHash
{
public string Type { get; set; }
public string Hash { get; set; }

public FileTypeHash(string type, string hash)
{
Type = type;
Hash = hash;
}

public FileTypeHash(string data)
{
string[] split = data.Split('|');
Type = split[0];
Hash = split[1];
}

public override string ToString()
{
return Type + "|" + Hash;
}
}

public class DllItemInfo
{
public string Export { get; set; }
public int? Index { get; set; }
public string Field { get; set; }
public string Label { get; set; }

public override string ToString()
{
StringBuilder sb = new StringBuilder(Export);
if (Index.HasValue)
sb.Append(Index.Value);
if (!string.IsNullOrEmpty(Field))
sb.AppendFormat("->{0}", Field);
return sb.ToString();
}
}
}
77 changes: 9 additions & 68 deletions DLLModGenerator/MainForm.cs
Expand Up @@ -32,7 +32,7 @@ public MainForm()
{ "actionarray", "NJS_ACTION **" }
};

MyClass IniData;
DllIniData IniData;

private void MainForm_Load(object sender, EventArgs e)
{
Expand Down Expand Up @@ -70,7 +70,7 @@ private void recentProjectsToolStripMenuItem_DropDownItemClicked(object sender,

private void LoadINI(string filename)
{
IniData = IniSerializer.Deserialize<MyClass>(filename);
IniData = IniSerializer.Deserialize<DllIniData>(filename);
if (Settings.MRUList.Contains(filename))
{
recentProjectsToolStripMenuItem.DropDownItems.RemoveAt(Settings.MRUList.IndexOf(filename));
Expand All @@ -80,7 +80,7 @@ private void LoadINI(string filename)
recentProjectsToolStripMenuItem.DropDownItems.Insert(0, new ToolStripMenuItem(filename));
Environment.CurrentDirectory = Path.GetDirectoryName(filename);
listView1.BeginUpdate();
foreach (KeyValuePair<string, FileTypeHash> item in IniData.Files.Items)
foreach (KeyValuePair<string, FileTypeHash> item in IniData.Files)
{
bool modified = HelperFunctions.FileHash(item.Key) != item.Value.Hash;
listView1.Items.Add(new ListViewItem(new[] { item.Key, modified ? "Yes" : "No" }) { Checked = modified });
Expand Down Expand Up @@ -128,20 +128,15 @@ private void button4_Click(object sender, EventArgs e)
if (fd.ShowDialog(this) == DialogResult.OK)
using (TextWriter writer = File.CreateText(fd.FileName))
{
bool SA2 = IniData.Game == Game.SA2 || IniData.Game == Game.SA2B;
bool SA2 = IniData.Game == Game.SA2B;
ModelFormat modelfmt = 0;
LandTableFormat landfmt = 0;
switch (IniData.Game)
{
case Game.SA1:
modelfmt = ModelFormat.Basic;
landfmt = LandTableFormat.SA1;
break;
case Game.SADX:
modelfmt = ModelFormat.BasicDX;
landfmt = LandTableFormat.SADX;
break;
case Game.SA2:
case Game.SA2B:
modelfmt = ModelFormat.Chunk;
landfmt = LandTableFormat.SA2;
Expand All @@ -156,7 +151,7 @@ private void button4_Click(object sender, EventArgs e)
writer.WriteLine();
List<string> labels = new List<string>();
int _i = 0;
foreach (KeyValuePair<string, FileTypeHash> item in IniData.Files.Items)
foreach (KeyValuePair<string, FileTypeHash> item in IniData.Files)
if (listView1.CheckedIndices.Contains(_i++))
switch (item.Value.Type)
{
Expand Down Expand Up @@ -190,72 +185,18 @@ private void button4_Click(object sender, EventArgs e)
writer.WriteLine("void __cdecl Init(const char *path, const HelperFunctions &helperFunctions)");
writer.WriteLine("{");
writer.WriteLine("\tHMODULE handle = GetModuleHandle(L\"{0}\");", IniData.Name);
foreach (KeyValuePair<string, string> item in IniData.ItemTypes.Items)
List<string> exports = new List<string>(IniData.Items.Where(item => labels.Contains(item.Label)).Select(item => item.Export));
foreach (KeyValuePair<string, string> item in IniData.Exports.Where(item => exports.Contains(item.Key)))
writer.WriteLine("\t{0}{1} = ({0})GetProcAddress(handle, \"{1}\");", typemap[item.Value], item.Key);
foreach (KeyValuePair<string, string> item in IniData.Labels.Items.Where((item) => labels.Contains(item.Value)))
writer.WriteLine("\t{0} = &{1};", item.Key, item.Value);
foreach (DllItemInfo item in IniData.Items.Where(item => labels.Contains(item.Label)))
writer.WriteLine("\t{0} = &{1};", item.ToString(), item.Label);
writer.WriteLine("}");
writer.WriteLine();
writer.WriteLine("extern \"C\" __declspec(dllexport) const ModInfo {0}ModInfo = {{ ModLoaderVer, Init, NULL, 0, NULL, 0, NULL, 0, NULL, 0 }};", SA2 ? "SA2" : "SADX");
}
}
}

public class MyClass
{
[IniName("name")]
public string Name { get; set; }
[IniAlwaysInclude]
[IniName("game")]
public Game Game { get; set; }
public DictionaryContainer<string> ItemTypes { get; set; }
public DictionaryContainer<FileTypeHash> Files { get; set; }
public DictionaryContainer<string> Labels { get; set; }

public MyClass()
{
ItemTypes = new DictionaryContainer<string>();
Files = new DictionaryContainer<FileTypeHash>();
Labels = new DictionaryContainer<string>();
}
}

public class DictionaryContainer<T>
{
[IniCollection(IniCollectionMode.IndexOnly)]
public Dictionary<string, T> Items { get; set; }

public DictionaryContainer()
{
Items = new Dictionary<string, T>();
}
}

[System.ComponentModel.TypeConverter(typeof(StringConverter<FileTypeHash>))]
public class FileTypeHash
{
public string Type { get; set; }
public string Hash { get; set; }

public FileTypeHash(string type, string hash)
{
Type = type;
Hash = hash;
}

public FileTypeHash(string data)
{
string[] split = data.Split('|');
Type = split[0];
Hash = split[1];
}

public override string ToString()
{
return Type + "|" + Hash;
}
}

static class Extensions
{
internal static List<string> GetLabels(this LandTable land)
Expand Down

0 comments on commit 9e513e6

Please sign in to comment.