Skip to content

Commit

Permalink
Updating INI handling to match other projects.
Browse files Browse the repository at this point in the history
  • Loading branch information
MainMemory committed May 7, 2015
1 parent 92f20fd commit c3ecc10
Show file tree
Hide file tree
Showing 14 changed files with 1,270 additions and 1,015 deletions.
4 changes: 2 additions & 2 deletions ObjDefEditor/MainForm.cs
Expand Up @@ -258,7 +258,7 @@ private void saveToolStripMenuItem_Click(object sender, EventArgs e)
Definition.Properties = null;
if (Definition.Subtypes.Items.Length == 0)
Definition.Subtypes = null;
IniFile.Serialize(objlist, objectLists[objectListList.SelectedIndex]);
IniSerializer.Serialize(objlist, objectLists[objectListList.SelectedIndex]);
Definition.Save(objlist[(string)objectDefinitionList.SelectedItem].XMLFile);
SetupDefinition();
}
Expand Down Expand Up @@ -305,7 +305,7 @@ private void objectListList_SelectedIndexChanged(object sender, EventArgs e)
addDefinitionButton.Enabled = false;
return;
}
objlist = IniFile.Deserialize<Dictionary<string, ObjectData>>(objectLists[objectListList.SelectedIndex]);
objlist = IniSerializer.Deserialize<Dictionary<string, ObjectData>>(objectLists[objectListList.SelectedIndex]);
objectDefinitionList.Items.Clear();
objectDefinitionList.BeginUpdate();
foreach (KeyValuePair<string, ObjectData> item in objlist)
Expand Down
4 changes: 2 additions & 2 deletions ObjectLayoutDiff/Program.cs
Expand Up @@ -53,10 +53,10 @@ static void Main(string[] args)
diff.removed = origobjs.Select(a => a.Data).ToList();
else
diff.removed = null;
IniFile.Serialize(diff, args[4]);
IniSerializer.Serialize(diff, args[4]);
break;
case "apply":
diff = IniFile.Deserialize<DiffData>(args[2]);
diff = IniSerializer.Deserialize<DiffData>(args[2]);
if (diff.format != format)
{
Console.WriteLine("Error: Diff file does not match selected format.");
Expand Down
2 changes: 1 addition & 1 deletion SonAni/MainForm.cs
Expand Up @@ -753,7 +753,7 @@ public class ProjectFile

public static ProjectFile Load(string filename)
{
ProjectFile result = IniFile.Deserialize<ProjectFile>(filename);
ProjectFile result = IniSerializer.Deserialize<ProjectFile>(filename);
if (result.MappingsGame == EngineVersion.Invalid)
result.MappingsGame = result.Game;
if (result.DPLCGame == EngineVersion.Invalid)
Expand Down
2 changes: 1 addition & 1 deletion SonED2ObjDefConverter/Program.cs
Expand Up @@ -159,7 +159,7 @@ static void Main(string[] args)
}
}
foreach (KeyValuePair<string, Dictionary<string, ObjectData>> item in objectinis)
IniFile.Serialize(item.Value, Path.Combine(outdir, item.Key == "FF" ? "obj.ini" : "obj" + item.Key + ".ini"));
IniSerializer.Serialize(item.Value, Path.Combine(outdir, item.Key == "FF" ? "obj.ini" : "obj" + item.Key + ".ini"));
}

public static Bitmap LoadPCXFile(string filename)
Expand Down
2 changes: 1 addition & 1 deletion SonED2ProjectConverter/Program.cs
Expand Up @@ -495,7 +495,7 @@ static int ReadObjectDefinition(string filename)
}
}
foreach (KeyValuePair<string, Dictionary<string, ObjectData>> item in objectinis)
IniFile.Serialize(item.Value, Path.Combine(outdir, item.Key == "FF" ? Path.GetFileName(filename) + ".ini" : Path.GetFileName(filename) + item.Key + ".ini"));
IniSerializer.Serialize(item.Value, Path.Combine(outdir, item.Key == "FF" ? Path.GetFileName(filename) + ".ini" : Path.GetFileName(filename) + item.Key + ".ini"));
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion SonLVL/AnimationInfo.cs
Expand Up @@ -46,7 +46,7 @@ public class AnimationInfo
string userfile = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + ".user" + Path.GetExtension(filename));
if (File.Exists(userfile))
ini = IniFile.Combine(ini, IniFile.Load(userfile));
Dictionary<string, AnimationInfo> result = IniFile.Deserialize<Dictionary<string, AnimationInfo>>(filename);
Dictionary<string, AnimationInfo> result = IniSerializer.Deserialize<Dictionary<string, AnimationInfo>>(filename);
foreach (KeyValuePair<string, AnimationInfo> anim in result)
{
if (anim.Value.MappingsGame == EngineVersion.Invalid)
Expand Down
10 changes: 5 additions & 5 deletions SonLVL/MainForm.cs
Expand Up @@ -131,7 +131,7 @@ private void MainForm_Load(object sender, EventArgs e)
Dictionary<string, int> downloaded;
Dictionary<string, UpdateInfo> updini;
if (File.Exists("Updater.ini"))
downloaded = IniFile.Deserialize<Dictionary<string, int>>("Updater.ini");
downloaded = IniSerializer.Deserialize<Dictionary<string, int>>("Updater.ini");
else
downloaded = new Dictionary<string, int>();
#if !DEBUG
Expand All @@ -142,7 +142,7 @@ private void MainForm_Load(object sender, EventArgs e)
{
string updatefile = Path.GetTempFileName();
cli.DownloadFile("http://mm.reimuhakurei.net/SonLVL/update.ini", updatefile);
updini = IniFile.Deserialize<Dictionary<string, UpdateInfo>>(updatefile);
updini = IniSerializer.Deserialize<Dictionary<string, UpdateInfo>>(updatefile);
File.Delete(updatefile);
}
List<string> updates = new List<string>();
Expand Down Expand Up @@ -181,11 +181,11 @@ private void MainForm_Load(object sender, EventArgs e)
imageTransparency.SetColorMatrix(x, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
string HUDpath = Path.Combine(Application.StartupPath, "HUD");
HUDLetters = new Dictionary<char, BitmapBits>();
Dictionary<char, string> huditems = IniFile.Deserialize<Dictionary<char, string>>(Path.Combine(HUDpath, "HUD.ini"));
Dictionary<char, string> huditems = IniSerializer.Deserialize<Dictionary<char, string>>(Path.Combine(HUDpath, "HUD.ini"));
foreach (KeyValuePair<char, string> item in huditems)
HUDLetters.Add(item.Key, new BitmapBits(Path.Combine(HUDpath, item.Value + ".png")));
HUDNumbers = new Dictionary<char, BitmapBits>();
huditems = IniFile.Deserialize<Dictionary<char, string>>(Path.Combine(HUDpath, "HUDnum.ini"));
huditems = IniSerializer.Deserialize<Dictionary<char, string>>(Path.Combine(HUDpath, "HUDnum.ini"));
foreach (KeyValuePair<char, string> item in huditems)
HUDNumbers.Add(item.Key, new BitmapBits(Path.Combine(HUDpath, item.Value + ".png")));
objectsAboveHighPlaneToolStripMenuItem.Checked = Settings.ObjectsAboveHighPlane;
Expand Down Expand Up @@ -652,7 +652,7 @@ private void backgroundLevelLoader_RunWorkerCompleted(object sender, RunWorkerCo
for (int i = 1; i < LevelData.PalName.Count; i++)
selectPaletteToolStripMenuItem.DropDownItems.Add(new ToolStripMenuItem(LevelData.PalName[i]) { Checked = LevelData.Level.WaterPalette - 1 == i, Tag = i });
waterPalette = LevelData.Level.WaterPalette < 2 ? -1 : LevelData.Level.WaterPalette - 1;
waterHeight = LevelData.Level.WaterHeight;
waterHeight = (ushort)LevelData.Level.WaterHeight;
if (waterPalette != -1)
{
LevelImgPalette.Entries[128] = LevelData.Palette[waterPalette][2, 0].RGBColor;
Expand Down
4 changes: 2 additions & 2 deletions SonLVL/Settings.cs
Expand Up @@ -64,7 +64,7 @@ public static Settings Load()
{
filename = Path.Combine(Application.StartupPath, "SonLVL.ini");
if (File.Exists(filename))
return IniFile.Deserialize<Settings>(filename);
return IniSerializer.Deserialize<Settings>(filename);
else
{
Settings result = new Settings();
Expand Down Expand Up @@ -92,7 +92,7 @@ public static Settings Load()

public void Save()
{
IniFile.Serialize(this, filename);
IniSerializer.Serialize(this, filename);
}
}

Expand Down
145 changes: 129 additions & 16 deletions SonLVLAPI/GameInfo.cs
Expand Up @@ -29,12 +29,7 @@ public class GameInfo
[IniName("blockcmp")]
public CompressionType BlockCompression { get; set; }
[IniName("blockmax")]
public string BlockMaxString
{
get { return BlockMax.HasValue ? BlockMax.Value.ToString("X") : null; }
set { BlockMax = value == null ? null : (ushort?)ushort.Parse(value, NumberStyles.HexNumber); }
}
[IniIgnore]
[TypeConverter(typeof(UInt16HexConverter))]
public ushort? BlockMax { get; set; }
[IniName("chunkfmt")]
public EngineVersion ChunkFormat { get; set; }
Expand Down Expand Up @@ -114,7 +109,7 @@ public static GameInfo Load(string filename)
string userfile = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename) + ".user" + Path.GetExtension(filename));
if (File.Exists(userfile))
ini = IniFile.Combine(ini, IniFile.Load(userfile));
GameInfo result = IniFile.Deserialize<GameInfo>(ini);
GameInfo result = IniSerializer.Deserialize<GameInfo>(ini);
if (result.MappingsVersion == EngineVersion.Invalid)
result.MappingsVersion = result.EngineVersion;
if (result.DPLCVersion == EngineVersion.Invalid)
Expand All @@ -124,7 +119,7 @@ public static GameInfo Load(string filename)

public void Save(string filename)
{
IniFile.Serialize(this, filename);
IniSerializer.Serialize(this, filename);
}

public LevelInfo GetLevelInfo(string levelName)
Expand Down Expand Up @@ -315,15 +310,10 @@ public class LevelInfo
[DefaultValue(0)]
[IniName("waterpal")]
public int WaterPalette { get; set; }
[DefaultValue("600")]
[DefaultValue(0x600)]
[IniName("waterheight")]
public string WaterHeightString
{
get { return WaterHeight.ToString("X"); }
set { WaterHeight = ushort.Parse(value, NumberStyles.HexNumber); }
}
[IniIgnore]
public ushort WaterHeight { get; set; }
[TypeConverter(typeof(Int32HexConverter))]
public int WaterHeight { get; set; }
[IniName("objectfmt")]
public EngineVersion ObjectFormat { get; set; }
[IniName("objectcmp")]
Expand Down Expand Up @@ -567,4 +557,127 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
return base.ConvertFrom(context, culture, value);
}
}

public class Int32HexConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is int)
return ((int)value).ToString("X");
return base.ConvertTo(context, culture, value, destinationType);
}

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
return int.Parse((string)value, NumberStyles.HexNumber);
return base.ConvertFrom(context, culture, value);
}

public override bool IsValid(ITypeDescriptorContext context, object value)
{
if (value is int)
return true;
int i;
if (value is string)
return int.TryParse((string)value, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out i);
return base.IsValid(context, value);
}
}

public class UInt16HexConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is ushort)
return ((ushort)value).ToString("X");
return base.ConvertTo(context, culture, value, destinationType);
}

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
return ushort.Parse((string)value, NumberStyles.HexNumber);
return base.ConvertFrom(context, culture, value);
}

public override bool IsValid(ITypeDescriptorContext context, object value)
{
if (value is ushort)
return true;
ushort i;
if (value is string)
return ushort.TryParse((string)value, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out i);
return base.IsValid(context, value);
}
}

public class ByteHexConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is byte)
return ((byte)value).ToString("X");
return base.ConvertTo(context, culture, value, destinationType);
}

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
return byte.Parse((string)value, NumberStyles.HexNumber);
return base.ConvertFrom(context, culture, value);
}

public override bool IsValid(ITypeDescriptorContext context, object value)
{
if (value is byte)
return true;
byte i;
if (value is string)
return byte.TryParse((string)value, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out i);
return base.IsValid(context, value);
}
}
}

0 comments on commit c3ecc10

Please sign in to comment.