Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Initial WIP status.
Browse files Browse the repository at this point in the history
  • Loading branch information
W Batt committed Sep 26, 2016
1 parent f9ec683 commit d30a239
Show file tree
Hide file tree
Showing 26 changed files with 7,466 additions and 0 deletions.
22 changes: 22 additions & 0 deletions TerrariaUSaveEditor.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TerrariaUSaveEditor", "TerrariaUSaveEditor\TerrariaUSaveEditor.csproj", "{289EE2AD-A1C9-479A-9425-CE9F384374DC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{289EE2AD-A1C9-479A-9425-CE9F384374DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{289EE2AD-A1C9-479A-9425-CE9F384374DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{289EE2AD-A1C9-479A-9425-CE9F384374DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{289EE2AD-A1C9-479A-9425-CE9F384374DC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions TerrariaUSaveEditor/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
39 changes: 39 additions & 0 deletions TerrariaUSaveEditor/Helper/ArrayHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TerrariaUSaveEditor.Helper
{
public static class ArrayHelper
{
public static byte[] RemoveRange(byte[] data, int index, int length)
{
var buffer = data.ToList();
buffer.RemoveRange(index, length);

return buffer.ToArray();
}

public static byte[] AddRangeAtIndex(byte[] data, byte[] extraData, int index)
{
var buffer = data.ToList();
buffer.InsertRange(index, extraData.ToList());

return buffer.ToArray();
}

public static byte[] ColorToByteArray(Color color)
{
byte[] colorArray = new byte[3];
colorArray[0] = color.R;
colorArray[1] = color.G;
colorArray[2] = color.B;

return colorArray;
}
}
}
87 changes: 87 additions & 0 deletions TerrariaUSaveEditor/Helper/ItemHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TerrariaUSaveEditor.Helper
{
public static class ItemHelper
{
public static List<InventoryData> GetItems(byte[] data, SlotType slotType)
{
List<InventoryData> items = new List<InventoryData>();
ushort slot = 0;

for (int i = 0; i < data.Length; i+=5)
{
var idArray = new byte[2];
Array.Copy(data, i, idArray, 0, 2);

if (IsEmptySlot(idArray))
{
i -= 3;
}

int id = GetIDFromBytes(idArray);

items.Add(new InventoryData()
{
Slot = slot,
Item = Items.GetItem(id),
SlotType = slotType,
Prefix = (ushort)data[i + 4],
Amount = (ushort)data[i + 3]
});

slot++;

if (GotAllSlots(slotType, slot))
{
break;
}
}

return items;
}

private static bool GotAllSlots(SlotType slotType, int currentSlot)
{
switch (slotType)
{
case SlotType.Bar:
return currentSlot == 10 ? true : false;
case SlotType.Inv:
return currentSlot == 30 ? true : false;
case SlotType.Ammo:
return currentSlot == 4 ? true : false;
case SlotType.Money:
return currentSlot == 4 ? true : false;
default:
throw new ArgumentException("Invalid SlotType");
}
}

private static bool IsEmptySlot(byte[] idData)
{
if (idData[0] == 0 &&
idData[1] == 0)
{
return true;
}

return false;
}

private static int GetIDFromBytes(byte[] idData)
{
if (idData[1] == 0)
{
return BitConverter.ToInt16(idData, 0);
}

Array.Reverse(idData);
return BitConverter.ToInt16(idData, 0);
}
}
}
60 changes: 60 additions & 0 deletions TerrariaUSaveEditor/Helper/OffsetHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;

namespace TerrariaUSaveEditor.Helper
{
public static class OffsetHelper
{
// Name length change all offsets.
public static int NameLength { get; set; }

// Empty slots get written as "00 00". Used slots uses 5 bytes. "ID ID ?? AMOUNT PREFIX"
public static int InventoryLength { get; set; }

private static int OffsetDiff => Name + NameLength;

// Character Offsets
public static int NameLengthInformation => 0x02;
public static int Name => 0x03;
public static int Difficutly => OffsetDiff + 0x00;
public static int Gender => OffsetDiff + 0x02;

// Color Offsets
public static int HairColor => OffsetDiff + 0x0B;
public static int BodyColor => HairColor + 0x03;
public static int EyeColor => BodyColor + 0x03;
public static int ShirtColor => EyeColor + 0x03;
public static int UnderShirtColor => ShirtColor + 0x03;
public static int PantsColor => UnderShirtColor + 0x03;
public static int ShoesColor => PantsColor + 0x03;

// Equip Offsets
/*
* Equip get saved right after Colors. 1 Accessory take 3 Bytes "ID ID ?PREFIX?"
* Again 2 Bytes as "00 00" if the slot is empty.
* Based on this informations for accessory's, equipable items like armor should behave the same.
* */

// Inventory Offsets
public static int Inventory => OffsetDiff + 0x03D;

public static int CalcOffsetDiffInventory(List<InventoryData> invData)
{
int usedSlots = 0;
int emptySlots = 0;
foreach (var item in invData)
{
if (item.Item.Id != 0)
{
usedSlots++;
}
else
{
emptySlots++;
}
}

return (emptySlots * 2) + (usedSlots * 5);
}
}
}
43 changes: 43 additions & 0 deletions TerrariaUSaveEditor/Helper/ValidationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace TerrariaUSaveEditor
{
public static class ValidationHelper
{
public static bool IsNameValid(string name, out string error)
{
error = "OK";
if (string.IsNullOrEmpty(name))
{
error = "Name can't be empty.";
return false;
}

if (name.Length > 16)
{
error = "Name is longer then 16 chars.";
return false;
}

if (!Regex.Match(name, @"^[a-zA-Z 0-9\?\!\*]*$").Success)
{
error = "Only a-z, A-Z, 0-9, ?, * and ! are allowed chars.";
//error = $"Only a-z, A-Z, 0-9, ?, * and !{Environment.NewLine} are allowed chars.";
return false;
}

return true;
}

public static bool IsNameValid(string name)
{
string error = string.Empty;
return IsNameValid(name, out error);
}
}
}
42 changes: 42 additions & 0 deletions TerrariaUSaveEditor/InventoryData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TerrariaUSaveEditor
{
public enum SlotType
{
Bar,
Inv,
Ammo,
Money
}

public class InventoryData
{
public ushort Slot { get; set; }

public SlotType SlotType { get; set; }

public ItemData Item { get; set; }

public int Prefix { get; set; }

public ushort Amount { get; set; }

public InventoryData()
{
}

public InventoryData(ushort slot, SlotType slotType)
{
this.Slot = slot;
this.SlotType = slotType;
this.Item = Items.GetItem(0);
this.Prefix = 0;
this.Amount = 0;
}
}
}
43 changes: 43 additions & 0 deletions TerrariaUSaveEditor/ItemBox.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions TerrariaUSaveEditor/ItemBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TerrariaUSaveEditor
{
public partial class ItemBox : Panel
{
public ItemBox()
{
this.InitializeComponent();
this.BorderStyle = BorderStyle.FixedSingle;
this.BackColor = Color.FromArgb(0 ,178, 238);
this.Size = new Size(45, 45);
}

public ItemBox(bool topBar)
: this()
{
if (topBar)
{
this.BackColor = Color.FromArgb(0, 191, 255);
}
}
}
}
Loading

0 comments on commit d30a239

Please sign in to comment.