Skip to content

Commit

Permalink
Various changes
Browse files Browse the repository at this point in the history
Add BC4/5/6 decoding
Show progress bars
Update enemy stats
Handle reference BDAT arrays with codegen
Add option to generate many things at once
Rearrange classes
Update BDAT references
  • Loading branch information
Thealexbarney committed Jun 18, 2018
1 parent 5c737c8 commit affd455
Show file tree
Hide file tree
Showing 71 changed files with 3,153 additions and 558 deletions.
1 change: 1 addition & 0 deletions XbTool/SaveEditor/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using XbTool;
using XbTool.Bdat;
using XbTool.Common;
using XbTool.Save;
using XbTool.Serialization;
using XbTool.Types;
Expand Down
12 changes: 7 additions & 5 deletions XbTool/XbTool/Bdat/BdatFieldInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using System.Linq;
using CsvHelper;
using CsvHelper.Configuration;
using XbTool.Common;

namespace XbTool.Bdat
{
[DebuggerDisplay("{" + nameof(DebugString) + ", nq}")]
public class BdatFieldInfo
{
public BdatFieldType Type { get; set; }
public BdatMember Member { get; set; }
public string Table { get; set; }
public string TableType { get; set; }
public string Field { get; set; }
Expand Down Expand Up @@ -65,7 +67,7 @@ public static class BdatInfoImport
{
public static Dictionary<(string table, string member), BdatFieldInfo> ReadBdatFieldInfo(string prefix)
{
using (var stream = new FileStream($"{prefix}_fieldInfo.csv", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var stream = Helpers.OpenDataFile($"{prefix}_fieldInfo.csv"))
using (var reader = new StreamReader(stream))
{
IEnumerable<BdatFieldInfo> csv = new CsvReader(reader, new Configuration { HeaderValidated = null, MissingFieldFound = null }).GetRecords<BdatFieldInfo>();
Expand All @@ -87,9 +89,9 @@ public static class BdatInfoImport
public static BdatArrayInfo[] ReadBdatArrayInfo(string prefix)
{
var info = new List<BdatArrayInfo>();
if (!File.Exists($"{prefix}_arrays.csv")) return info.ToArray();
FileStream stream = Helpers.TryOpenDataFile($"{prefix}_arrays.csv");
if (stream == null) return info.ToArray();

using (var stream = new FileStream($"{prefix}_arrays.csv", FileMode.Open, FileAccess.Read))
using (var reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
Expand Down Expand Up @@ -118,9 +120,9 @@ public static BdatArrayInfo[] ReadBdatArrayInfo(string prefix)
public static Dictionary<string, string> ReadBdatTableInfo(string prefix)
{
var display = new Dictionary<string, string>();
if (!File.Exists($"{prefix}_tableInfo.csv")) return display;
FileStream stream = Helpers.TryOpenDataFile($"{prefix}_tableinfo.csv");
if (stream == null) return display;

using (var stream = new FileStream($"{prefix}_tableInfo.csv", FileMode.Open, FileAccess.Read))
using (var reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
Expand Down
1 change: 1 addition & 0 deletions XbTool/XbTool/Bdat/BdatMember.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using XbTool.Common;

namespace XbTool.Bdat
{
Expand Down
1 change: 1 addition & 0 deletions XbTool/XbTool/Bdat/BdatTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using XbTool.Common;

namespace XbTool.Bdat
{
Expand Down
1 change: 1 addition & 0 deletions XbTool/XbTool/Bdat/BdatTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Text.RegularExpressions;
using XbTool.CodeGen;
using XbTool.Common;

namespace XbTool.Bdat
{
Expand Down
1 change: 1 addition & 0 deletions XbTool/XbTool/Bdat/BdatTools.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using XbTool.Common;

namespace XbTool.Bdat
{
Expand Down
1 change: 1 addition & 0 deletions XbTool/XbTool/BdatString/BdatStringCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using XbTool.Bdat;
using XbTool.Common;

namespace XbTool.BdatString
{
Expand Down
1 change: 1 addition & 0 deletions XbTool/XbTool/BdatString/BdatStringTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Text;
using XbTool.Common;
using XbTool.Types;

namespace XbTool.BdatString
Expand Down
1 change: 1 addition & 0 deletions XbTool/XbTool/BdatString/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using XbTool.Bdat;
using XbTool.Common;
using XbTool.Types;

namespace XbTool.BdatString
Expand Down
1 change: 1 addition & 0 deletions XbTool/XbTool/CliArguments.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Reflection;
using XbTool.Common;

namespace XbTool
{
Expand Down
106 changes: 74 additions & 32 deletions XbTool/XbTool/CodeGen/SerializationCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static string GenerateTypesFile(BdatTables info)
sb.AppendLine();
sb.AppendLine("using System;");
sb.AppendLine();
sb.AppendLine("using XbTool.Types");
sb.AppendLine("namespace XbTool.Types");
sb.AppendLineAndIncrease("{");

for (int i = 0; i < info.Types.Length; i++)
Expand Down Expand Up @@ -88,27 +88,43 @@ private static void GenerateType(BdatType type, Indenter sb)

foreach (var bdatRef in type.TableRefs)
{
bdatRef.Member = type.Members.First(x => x.Name == bdatRef.Field);
string itemType = "";
string itemName = bdatRef.Field;

switch (bdatRef.Type)
{
case BdatFieldType.Reference:
case BdatFieldType.Message:
sb.AppendLine($"public {bdatRef.ChildType} _{bdatRef.Field};");
itemType = bdatRef.ChildType;
break;
case BdatFieldType.Item:
case BdatFieldType.Task:
case BdatFieldType.Condition:
sb.AppendLine($"public object _{bdatRef.Field};");
itemType = "object";
break;
case BdatFieldType.Enum:
sb.AppendLine($"public {bdatRef.EnumType.Name} _{bdatRef.Field};");
itemType = bdatRef.EnumType.Name;
break;
default:
if (bdatRef.EnumType != null)
{
sb.AppendLine($"public {bdatRef.EnumType.Name} _{bdatRef.Field};");
itemType = bdatRef.EnumType.Name;
}
break;
}

switch (bdatRef.Member.Type)
{
case BdatMemberType.Scalar:
sb.AppendLine($"public {itemType} _{itemName};");
break;
case BdatMemberType.Array:
sb.AppendLine($"public {itemType}[] _{itemName} = new {itemType}[{bdatRef.Member.ArrayCount}];");
break;
default:
throw new ArgumentOutOfRangeException();
}
}

foreach (var array in type.Arrays)
Expand All @@ -128,7 +144,7 @@ private static string GenerateReadFunctionsFile(BdatTables info)
sb.AppendLine("// ReSharper disable UseObjectOrCollectionInitializer");
sb.AppendLine("// ReSharper disable UnusedParameter.Global").AppendLine();
sb.AppendLine("using System;");
sb.AppendLine("using XbTool.Types").AppendLine();
sb.AppendLine("using XbTool.Types;").AppendLine();
sb.AppendLine("namespace XbTool.Serialization");
sb.AppendLineAndIncrease("{");
sb.AppendLine("public static class ReadFunctions");
Expand Down Expand Up @@ -200,32 +216,58 @@ private static void GenerateSetReferencesTable(Indenter sb, BdatTableDesc table)
sb.AppendLineAndIncrease("{");
foreach (var fieldRef in table.TableRefs.OrderBy(x => x.Field))
{
switch (fieldRef.Type)
if (fieldRef.Member?.Type == BdatMemberType.Array)
{
case BdatFieldType.Reference:
sb.AppendLine($"item._{fieldRef.Field} = tables.{fieldRef.RefTable}.GetItemOrNull(item.{PrintFieldRefId(fieldRef)});");
break;
case BdatFieldType.Message:
sb.AppendLine($"item._{fieldRef.Field} = tables.{fieldRef.RefTable}.GetItemOrNull(item.{PrintFieldRefId(fieldRef)});");
break;
case BdatFieldType.Item:
sb.AppendLine($"item._{fieldRef.Field} = tables.GetItem(item.{PrintFieldRefId(fieldRef)});");
break;
case BdatFieldType.Task:
sb.AppendLine($"item._{fieldRef.Field} = tables.GetTask((TaskType)item.{fieldRef.RefField}, item.{PrintFieldRefId(fieldRef)});");
break;
case BdatFieldType.Condition:
sb.AppendLine($"item._{fieldRef.Field} = tables.GetCondition((ConditionType)item.{fieldRef.RefField}, item.{PrintFieldRefId(fieldRef)});");
break;
case BdatFieldType.Enum:
sb.AppendLine($"item._{fieldRef.Field} = ({fieldRef.EnumType.Name})item.{fieldRef.Field};");
break;
default:
if (fieldRef.EnumType != null)
{
sb.AppendLine($"item._{fieldRef.Field} = ({fieldRef.EnumType.Name})item.{PrintFieldRefId(fieldRef)};");
}
break;
sb.AppendLine($"for (int i = 0; i < {fieldRef.Member.ArrayCount}; i++)");
sb.AppendLineAndIncrease("{");

switch (fieldRef.Type)
{
case BdatFieldType.Reference:
sb.AppendLine(
$"item._{fieldRef.Field}[i] = tables.{fieldRef.RefTable}.GetItemOrNull(item.{PrintFieldRefId(fieldRef)}[i]);");
break;
default:
throw new NotImplementedException();
}
sb.DecreaseAndAppendLine("}");
}
else
{
switch (fieldRef.Type)
{
case BdatFieldType.Reference:
sb.AppendLine(
$"item._{fieldRef.Field} = tables.{fieldRef.RefTable}.GetItemOrNull(item.{PrintFieldRefId(fieldRef)});");
break;
case BdatFieldType.Message:
sb.AppendLine(
$"item._{fieldRef.Field} = tables.{fieldRef.RefTable}.GetItemOrNull(item.{PrintFieldRefId(fieldRef)});");
break;
case BdatFieldType.Item:
sb.AppendLine(
$"item._{fieldRef.Field} = tables.GetItem(item.{PrintFieldRefId(fieldRef)});");
break;
case BdatFieldType.Task:
sb.AppendLine(
$"item._{fieldRef.Field} = tables.GetTask((TaskType)item.{fieldRef.RefField}, item.{PrintFieldRefId(fieldRef)});");
break;
case BdatFieldType.Condition:
sb.AppendLine(
$"item._{fieldRef.Field} = tables.GetCondition((ConditionType)item.{fieldRef.RefField}, item.{PrintFieldRefId(fieldRef)});");
break;
case BdatFieldType.Enum:
sb.AppendLine($"item._{fieldRef.Field} = ({fieldRef.EnumType.Name})item.{fieldRef.Field};");
break;
default:
if (fieldRef.EnumType != null)
{
sb.AppendLine(
$"item._{fieldRef.Field} = ({fieldRef.EnumType.Name})item.{PrintFieldRefId(fieldRef)};");
}

break;
}
}
}

Expand Down Expand Up @@ -289,7 +331,7 @@ private static string GenerateBdatCollectionFile(BdatTables info)
sb.AppendLine("// ReSharper disable UnusedMember.Global").AppendLine();
sb.AppendLine("using System;");
sb.AppendLine("using XbTool.Bdat;").AppendLine();
sb.AppendLine("using XbTool.Types");
sb.AppendLine("namespace XbTool.Types");
sb.AppendLineAndIncrease("{");
sb.AppendLine("[Serializable]");
sb.AppendLine("public class BdatCollection");
Expand Down
2 changes: 1 addition & 1 deletion XbTool/XbTool/Byte.cs → XbTool/XbTool/Common/Byte.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace XbTool
namespace XbTool.Common
{
public static class Byte
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Text;

namespace XbTool
namespace XbTool.Common
{
public class DataBuffer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Linq;

namespace XbTool
namespace XbTool.Common
{
public static class EnumExtensions
{
Expand Down
21 changes: 19 additions & 2 deletions XbTool/XbTool/Helpers.cs → XbTool/XbTool/Common/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.IO;

namespace XbTool
namespace XbTool.Common
{
public static class Helpers
{
Expand All @@ -27,7 +27,7 @@ private static object InitializeJaggedArray(Type type, int index, int[] lengths)
public static string GetRelativePath(string path, string basePath)
{
var directory = new DirectoryInfo(basePath);
var file = new System.IO.FileInfo(path);
var file = new FileInfo(path);

string fullDirectory = directory.FullName;
string fullFile = file.FullName;
Expand All @@ -39,5 +39,22 @@ public static string GetRelativePath(string path, string basePath)

return fullFile.Substring(fullDirectory.Length + 1);
}

public static FileStream TryOpenDataFile(string filename)
{
string path = null;
if (File.Exists(filename)) path = filename;

var localPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", filename);
if (File.Exists(localPath)) path = localPath;

if (path == null) return null;
return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}

public static FileStream OpenDataFile(string filename)
{
return TryOpenDataFile(filename) ?? throw new FileNotFoundException($"Could not find data file {filename}", filename);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;

namespace XbTool
namespace XbTool.Common
{
public interface IFileReader
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
namespace XbTool
namespace XbTool.Common
{
public interface IProgressReport
{
/// <summary>
/// Sets the current value of the <see cref="IProgressReport"/> to <paramref name="value"/>.
/// </summary>
/// <param name="value">The value to set.</param>
void Report(int value);
void Report(long value);

/// <summary>
/// Adds <paramref name="value"/> to the current value of the <see cref="IProgressReport"/>.
/// </summary>
/// <param name="value">The amount to add.</param>
void ReportAdd(int value);
void ReportAdd(long value);

/// <summary>
/// Sets the maximum value of the <see cref="IProgressReport"/> to <paramref name="value"/>.
/// </summary>
/// <param name="value">The maximum value to set.</param>
void SetTotal(int value);
void SetTotal(long value);

/// <summary>
/// Logs a message to the <see cref="IProgressReport"/> object.
Expand Down
Loading

0 comments on commit affd455

Please sign in to comment.