Skip to content

Commit

Permalink
added a few new experimental features, fixed villager house being inc…
Browse files Browse the repository at this point in the history
…orrect the next day
  • Loading branch information
berichan committed Jul 6, 2020
1 parent 09af7ec commit 5d10b66
Show file tree
Hide file tree
Showing 9 changed files with 1,779 additions and 141 deletions.
1,735 changes: 1,612 additions & 123 deletions ACNHMS_Source/Assets/Scenes/MainScene.unity

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class UI_MoneyMiles : IUI_Additional
public static string MoneyValueAddress = OffsetHelper.BankAddress.ToString("X"); // ABA86BC4
public static string MilesAddress = OffsetHelper.MilesAddress.ToString("X"); // has miles current then miles total after it :) ABA2DD28
public static string WalletAddress = OffsetHelper.WalletAddress.ToString("X"); // has storage a bit after it in ram ABA52760
public static uint CurrentMoneyAddress { get { return StringUtil.GetHexValue(MoneyValueAddress); } }
public static uint CurrentMilesAddress { get { return StringUtil.GetHexValue(MilesAddress); } }
public static uint CurrentWalletAddress { get { return StringUtil.GetHexValue(WalletAddress); } }
public static uint CurrentMoneyAddress { get { return StringUtil.GetHexValue(MoneyValueAddress) + (uint)(UI_Settings.GetPlayerIndex() * (uint)OffsetHelper.PlayerSize); } }
public static uint CurrentMilesAddress { get { return StringUtil.GetHexValue(MilesAddress) + (uint)(UI_Settings.GetPlayerIndex() * (uint)OffsetHelper.PlayerSize); ; } }
public static uint CurrentWalletAddress { get { return StringUtil.GetHexValue(WalletAddress) + (uint)(UI_Settings.GetPlayerIndex() * (uint)OffsetHelper.PlayerSize); ; } }

public InputField BankInput, PouchInput, MilesInput, MilesTotalInput;
public InputField MoneyAddressInput, PouchAddressInput, MilesAddressInput;
Expand Down
39 changes: 31 additions & 8 deletions ACNHMS_Source/Assets/Scripts/ACNHMS/AdditionalPanel/UI_Villager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class UI_Villager : IUI_Additional
public UI_VillagerSelect Selector;

private Villager loadedVillager;
private VillagerHouse loadedVillagerHouse;
private List<VillagerHouse> loadedVillagerHouses;
private SpriteParser villagerSprites;
private bool loadedVillagerShells = false;
private int currentlyLoadedVillagerIndex = -1;
Expand Down Expand Up @@ -64,11 +64,27 @@ private void loadAllVillagers()
{
byte[] loaded = CurrentConnection.ReadBytes(CurrentVillagerAddress + (uint)(i * Villager.SIZE), 3);
Villager villagerShell = new Villager(loaded);
if (villagerShell.Species == (byte)VillagerSpecies.non)
{
TenVillagers[i].GetComponent<Button>().enabled = false;
continue;
}
else
TenVillagers[i].GetComponent<Button>().enabled = true;

Texture2D pic = SpriteBehaviour.PullTextureFromParser(villagerSprites, villagerShell.InternalName);
if (pic != null)
TenVillagers[i].texture = pic;
}

// load all houses
loadedVillagerHouses = new List<VillagerHouse>();
byte[] houses = CurrentConnection.ReadBytes(CurrentVillagerHouseAddress, VillagerHouse.SIZE * 10);
for (int i = 0; i < 10; ++i)
{
loadedVillagerHouses.Add(new VillagerHouse(houses.Slice(i * VillagerHouse.SIZE, VillagerHouse.SIZE)));
}

loadedVillagerShells = true;
BlockerRoot.gameObject.SetActive(false);
}
Expand All @@ -90,10 +106,6 @@ public void loadVillager(int index)
currentlyLoadedVillagerIndex = index;
loadedVillager = new Villager(loaded);

// get their house
byte[] loadedHouse = CurrentConnection.ReadBytes(CurrentVillagerHouseAddress + (uint)(currentlyLoadedVillagerIndex * VillagerHouse.SIZE), VillagerHouse.SIZE);
loadedVillagerHouse = new VillagerHouse(loadedHouse);

VillagerToUI(loadedVillager);
}
catch (Exception e)
Expand Down Expand Up @@ -132,7 +144,14 @@ private void setCurrentVillager(bool includeHouse)
CurrentConnection.WriteBytes(villager, CurrentVillagerAddress + (uint)(currentlyLoadedVillagerIndex * Villager.SIZE));

if (includeHouse)
CurrentConnection.WriteBytes(loadedVillagerHouse.Data, CurrentVillagerHouseAddress + (uint)(currentlyLoadedVillagerIndex * VillagerHouse.SIZE));
{
// send all houses
List<byte> linearHouseArray = new List<byte>();
foreach (VillagerHouse vh in loadedVillagerHouses)
linearHouseArray.AddRange(vh.Data);
CurrentConnection.WriteBytes(linearHouseArray.ToArray(), CurrentVillagerHouseAddress);
CurrentConnection.WriteBytes(linearHouseArray.ToArray(), CurrentVillagerHouseAddress + (uint)OffsetHelper.VillagerHouseBufferDiff); // there's a temporary day buffer
}


if (UI_ACItemGrid.LastInstanceOfItemGrid != null)
Expand Down Expand Up @@ -181,12 +200,16 @@ private void loadVillagerData()
Villager newV = new Villager(villagerDump);
newV.SetMemories(loadedVillager.GetMemories());
newV.CatchPhrase = GameInfo.Strings.GetVillagerDefaultPhrase(newVillager);

VillagerHouse newVH = new VillagerHouse(villagerHouse);
VillagerHouse loadedVillagerHouse = loadedVillagerHouses.Find(x => x.NPC1 == (sbyte)currentlyLoadedVillagerIndex); // non indexed so search for the correct one
newVH.NPC1 = loadedVillagerHouse.NPC1;


loadedVillager = newV;
loadedVillagerHouse = newVH;
int index = loadedVillagerHouses.IndexOf(loadedVillagerHouse);
if (index == -1)
throw new Exception("The villager being replaced doesn't have a house on your island.");
loadedVillagerHouses[index] = newVH;

SetCurrentVillager();
TenVillagers[currentlyLoadedVillagerIndex].texture = SpriteBehaviour.PullTextureFromParser(villagerSprites, newVillager);
Expand Down
6 changes: 4 additions & 2 deletions ACNHMS_Source/Assets/Scripts/ACNHMS/UI_ACItemGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,17 @@ private void AfterRead(InjectionResult r)
else
{
Debug.LogError(r.ToString());
PopupHelper.CreateError(r.ToString(), 2f);
if (r != InjectionResult.Same)
PopupHelper.CreateError(r.ToString(), 2f);
}
}

private static void AfterWrite(InjectionResult r)
{
Debug.Log($"Write result: {r}");
if (r != InjectionResult.Success)
PopupHelper.CreateError($"Write result: {r}", 2f);
if (r != InjectionResult.Same)
PopupHelper.CreateError($"Write result: {r}", 2f);
}

private void set(Item[] items)
Expand Down
50 changes: 50 additions & 0 deletions ACNHMS_Source/Assets/Scripts/ACNHMS/UI_Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using NHSE.Core;
using NHSE.Injection;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UI_Player : MonoBehaviour
{


public Text PlayerName;
public RawImage PlayerText;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

public static string[] FetchPlayerNames(IRAMReadWriter rw)
{
List<string> toRet = new List<string>();
for (int i = 0; i < 8; ++i)
{
ulong address = OffsetHelper.getPlayerIdAddress(SysBotController.CurrentOffsetFirstPlayerUInt) - 0xB8 + 0x20 + (OffsetHelper.PlayerSize*(ulong)i);
byte[] pName = rw.ReadBytes((uint)address, 20);
string name = string.Empty;
if (!isZeroArray(pName))
name = StringUtil.GetString(pName, 0, 10);
toRet.Add(name == string.Empty ? string.Format("No one ({0})", (char)((uint)'A' + i)) : name);
}

return toRet.ToArray();
}

private static bool isZeroArray(byte[] bytes)
{
for (int i = 0; i < bytes.Length; ++i)
if (bytes[i] != 0)
return false;
return true;
}
}
11 changes: 11 additions & 0 deletions ACNHMS_Source/Assets/Scripts/ACNHMS/UI_Player.cs.meta

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

52 changes: 51 additions & 1 deletion ACNHMS_Source/Assets/Scripts/ACNHMS/UI_Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ public class UI_Settings : MonoBehaviour
public const string ITEMLANGMODEKEY = "ITEMLMKEY";
public const string VALIDATADATAKEY = "VKEY";
public const string INJMODEKEY = "INJKEY";
public const string PLAYERINDEXKEY = "PINDKEY";

public Dropdown LanguageField;
public Dropdown SearchMode;
public Dropdown InjectionMode;
public Dropdown WhichPlayer;
public InputField Offset;
public Toggle ValidataData;

public Button FetchNamesButton;

// Start is called before the first frame update
void Start()
{
Expand Down Expand Up @@ -83,13 +87,27 @@ void Start()
Offset.onValueChanged.AddListener(delegate { SysBotController.CurrentOffset = Offset.text; });
ValidataData.onValueChanged.AddListener(delegate { SetValidateData(ValidataData.isOn); });

// player index
string[] choices = new string[8];
for (int i = 0; i < 8; ++i)
choices[i] = string.Format("Player {0}", (char)((uint)'A' + i)); // 'A' + i
generatePlayerIndexList(choices, GetPlayerIndex());

SetLanguage(GetLanguage());
}

// Update is called once per frame
float counter = 1; //hacky but check every second, OnEnable won't work here! 1 so it check immediately
void Update()
{

counter += Time.deltaTime;

if (counter > 1)
{
var rw = UI_ACItemGrid.LastInstanceOfItemGrid?.GetCurrentlyActiveReadWriter();
FetchNamesButton.gameObject.SetActive(rw != null);
counter = 0;
}
}

public static bool GetValidateData(bool defVal = true)
Expand Down Expand Up @@ -134,4 +152,36 @@ public static void SetLanguage(int nLang)
PlayerPrefs.SetInt(ITEMLANGMODEKEY, nLang);
GameInfo.SetLanguage2Char(nLang);
}

// player index
public static int GetPlayerIndex(int defPlayer = 0) => PlayerPrefs.GetInt(PLAYERINDEXKEY, defPlayer);

public static void SetPlayerIndex(int nVal) => PlayerPrefs.SetInt(PLAYERINDEXKEY, nVal);

public void FetchPlayerNames()
{
var rw = UI_ACItemGrid.LastInstanceOfItemGrid.GetCurrentlyActiveReadWriter();
string[] toPlace;
if (rw != null)
toPlace = UI_Player.FetchPlayerNames(rw);
else
return;

generatePlayerIndexList(toPlace, GetPlayerIndex());
}

private void generatePlayerIndexList(string[] values, int select = 0)
{
WhichPlayer.onValueChanged.RemoveAllListeners();
WhichPlayer.ClearOptions();
foreach (string sm in values)
{
Dropdown.OptionData newVal = new Dropdown.OptionData();
newVal.text = sm;
WhichPlayer.options.Add(newVal);
}
WhichPlayer.value = Mathf.Min(WhichPlayer.options.Count-1, select);
WhichPlayer.RefreshShownValue();
WhichPlayer.onValueChanged.AddListener(delegate { SetPlayerIndex(WhichPlayer.value); });
}
}
16 changes: 14 additions & 2 deletions ACNHMS_Source/Assets/Scripts/Tools/OffsetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@

public static class OffsetHelper
{
public const ulong InventoryOffset = 0xABA526A8;
// some helpers
public const ulong PlayerSize = 0x6D6D0;
public const ulong PlayerOtherStartPadding = 0x35E40;

// player other (Personal Offsets 13)
public const ulong InventoryOffset = 0xABA526A8; // player 0 (A)
private const ulong playerOtherStart = InventoryOffset - 0x10; // helps to get other values, unused

public const ulong WalletAddress = InventoryOffset + 0xB8;
public const ulong MilesAddress = InventoryOffset - 0x24980;
public const ulong BankAddress = InventoryOffset + 0x3451C;

// main player offsets functions
private static ulong getPlayerStart(ulong invOffset) => invOffset - 0x10 - PlayerOtherStartPadding + 0x110;
public static ulong getPlayerIdAddress(ulong invOffset) => getPlayerStart(invOffset) + 0xAFA8;


// main save offsets
public const ulong TurnipAddress = 0xAA890CB0;
public const ulong VillagerAddress = TurnipAddress - 0x411F40;
public const ulong VillagerHouseAddress = VillagerAddress + 0x40E228;
//public const ulong VillagerHouseBufferDiff = 0xACEDA0;
public const ulong VillagerHouseBufferDiff = 0xACEDA0;
//public const ulong HeapVillagerHouse = 0x2A9B94A8;
}
5 changes: 3 additions & 2 deletions ACNHMS_Source/Assets/Scripts/Tools/SysBotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class SysBotController
{
public static string CurrentOffset = OffsetHelper.InventoryOffset.ToString("X");// "ABA526A8";
public static uint CurrentOffsetFirstPlayerUInt { get => StringUtil.GetHexValue(CurrentOffset); }

public SysBotController(InjectionType type) => Type = type;

Expand Down Expand Up @@ -41,8 +42,8 @@ public bool Connect(string ip, string port, out string error)

public uint GetDefaultOffset()
{
uint toRet = StringUtil.GetHexValue(CurrentOffset);
return toRet;
var toRet = StringUtil.GetHexValue(CurrentOffset) + ((ulong)UI_Settings.GetPlayerIndex() * OffsetHelper.PlayerSize);
return (uint)toRet;
}

public void SetOffset(uint value)
Expand Down

0 comments on commit 5d10b66

Please sign in to comment.