Skip to content

Commit 302eb92

Browse files
committed
Change int to string
1 parent af2ecd7 commit 302eb92

8 files changed

Lines changed: 150 additions & 66 deletions

File tree

Common/Configs/Config.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public class Config : ModConfig
1818
[Header("ReloadOptions")]
1919

2020
[CustomModConfigItem(typeof(PlayerIndexSliderElement))]
21-
[DefaultValue(0)]
22-
public int Player = 0; // player index in Main.PlayerList
21+
[DefaultValue("")]
22+
public string Player; // player index in Main.PlayerList
2323

2424
[CustomModConfigItem(typeof(WorldIndexSliderElement))]
25-
public int World; // world index in Main.WorldList
25+
public string World; // world index in Main.WorldList
2626

2727
[DefaultValue(true)]
2828
public bool AutoJoinWorld;

Common/Configs/ConfigElements/IntOptionElement.cs renamed to Common/Configs/ConfigElements/IntPathOptionElement.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace ModReloader.Common.Configs.ConfigElements
77
{
8-
public abstract class IntOptionElement : RangeElement
8+
public abstract class IntPathOptionElement : RangeElement
99
{
1010
public override int NumberTicks
1111
{
@@ -62,17 +62,23 @@ public override void OnBind()
6262

6363
protected abstract string ResolveName(int index);
6464

65+
protected abstract string IDToPath(int index);
66+
67+
protected abstract int PathToID(string path);
68+
6569
private int ReadIndex()
6670
{
6771
object raw = MemberInfo.GetValue(Item);
68-
if (raw is int i) return i;
72+
int index = 0;
73+
if (raw is string path) index = PathToID(path);
74+
if (index >= 0) return index;
6975
return 0;
7076
}
7177

7278
private void WriteIndex(int i)
7379
{
7480
if (!MemberInfo.CanWrite) return;
75-
MemberInfo.SetValue(Item, i);
81+
MemberInfo.SetValue(Item, IDToPath(i));
7682
}
7783
}
7884
}

Common/Configs/ConfigElements/PlayerIndexSliderElement.cs

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace ModReloader.Common.Configs.ConfigElements;
1010

11-
public class PlayerIndexSliderElement : IntOptionElement
11+
public class PlayerIndexSliderElement : IntPathOptionElement
1212
{
1313
protected override int GetCount()
1414
{
@@ -23,7 +23,7 @@ protected override string ResolveName(int index)
2323
if (clamped < 0) clamped = 0;
2424
if (clamped > Main.PlayerList.Count - 1) clamped = Main.PlayerList.Count - 1;
2525
var file = Main.PlayerList[clamped];
26-
if (file != null && file.Player != null && !string.IsNullOrWhiteSpace(file.Player.name))
26+
if (file != null && file.Player != null && !string.IsNullOrWhiteSpace(file.Player.name))
2727
return file.Player.name;
2828
if (file != null && !string.IsNullOrWhiteSpace(file.Name)) return file.Name;
2929
return "Player " + clamped;
@@ -36,43 +36,57 @@ public override void Draw(SpriteBatch sb)
3636
// Player instance
3737
int playerIndex = -1;
3838
object raw = MemberInfo.GetValue(Item);
39-
if (raw is int i)
40-
playerIndex = i;
39+
if (raw is string path)
40+
{
41+
playerIndex = PathToID(path);
42+
}
4143

4244
if (playerIndex < 0)
43-
return;
45+
playerIndex = 0;
4446

45-
if (playerIndex != -1)
46-
{
47-
// Player
48-
var player = Main.PlayerList[playerIndex].Player;
49-
string name = player.name;
50-
51-
// Measure width
52-
var font = FontAssets.ItemStack.Value;
53-
var width = font.MeasureString(name).X;
54-
55-
// Dims
56-
var dims = GetDimensions();
57-
var rect = dims.ToRectangle();
58-
var namePos = new Vector2(rect.X + dims.Width - 200-width, rect.Y + 7);
59-
60-
ChatManager.DrawColorCodedStringWithShadow(sb, font,
61-
name, namePos, Color.White, 0f, Vector2.Zero, Vector2.One);
62-
63-
// Player head pos
64-
Vector2 headPos = new(namePos.X-20, namePos.Y+5);
65-
66-
// Draw player head
67-
PlayerHeadFlipHook.shouldFlipHeadDraw = player.direction == -1;
68-
Main.MapPlayerRenderer.DrawPlayerHead(
69-
Main.Camera,
70-
player,
71-
headPos,
72-
scale: 0.8f,
73-
borderColor: Color.White
74-
);
75-
PlayerHeadFlipHook.shouldFlipHeadDraw = false;
76-
}
47+
48+
// Dims
49+
var dims = GetDimensions();
50+
var rect = dims.ToRectangle();
51+
var pos = new Vector2(rect.X + dims.Width - 200, rect.Y + 12);
52+
// Player
53+
var player = Main.PlayerList[playerIndex].Player;
54+
string name = player.name;
55+
56+
// Measure width
57+
var font = FontAssets.ItemStack.Value;
58+
var width = font.MeasureString(name).X;
59+
var namePos = new Vector2(rect.X + dims.Width - 200 - width, rect.Y + 7);
60+
61+
ChatManager.DrawColorCodedStringWithShadow(sb, font,
62+
name, namePos, Color.White, 0f, Vector2.Zero, Vector2.One);
63+
64+
// Player head pos
65+
Vector2 headPos = new(namePos.X - 20, namePos.Y + 5);
66+
67+
// Draw player head
68+
PlayerHeadFlipHook.shouldFlipHeadDraw = player.direction == -1;
69+
Main.MapPlayerRenderer.DrawPlayerHead(
70+
Main.Camera,
71+
player,
72+
headPos,
73+
scale: 0.8f,
74+
borderColor: Color.White
75+
);
76+
PlayerHeadFlipHook.shouldFlipHeadDraw = false;
77+
78+
79+
}
80+
81+
protected override string IDToPath(int index)
82+
{
83+
return Utilities.FindPlayer(index).Path;
84+
}
85+
86+
protected override int PathToID(string path)
87+
{
88+
return Utilities.FindPlayerId(path);
89+
90+
7791
}
7892
}

Common/Configs/ConfigElements/WorldIndexSliderElement.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace ModReloader.Common.Configs.ConfigElements;
1010

11-
public class WorldIndexSliderElement : IntOptionElement
11+
public class WorldIndexSliderElement : IntPathOptionElement
1212
{
1313
private WorldPreviewElement worldPreviewElement;
1414

@@ -40,12 +40,18 @@ public override void Draw(SpriteBatch sb)
4040
{
4141
base.Draw(sb);
4242

43-
int worldIndex = MemberInfo.GetValue(Item) as int? ?? -1;
43+
var raw = MemberInfo.GetValue(Item);
44+
int worldIndex = 0;
45+
if (raw is string path)
46+
{
47+
worldIndex = PathToID(path);
48+
}
49+
4450
if (worldIndex < 0 || Main.WorldList == null || worldIndex >= Main.WorldList.Count)
4551
return;
4652

4753
var world = Main.WorldList[worldIndex];
48-
string name = world.Name;
54+
string name = world.GetWorldName();
4955

5056
// Measure width for positioning text
5157
var font = FontAssets.ItemStack.Value;
@@ -84,4 +90,14 @@ public override void Draw(SpriteBatch sb)
8490
worldPreviewElement.Recalculate();
8591
worldPreviewElement.Draw(sb);
8692
}
93+
94+
protected override string IDToPath(int index)
95+
{
96+
return Utilities.FindWorld(index).Path;
97+
}
98+
99+
protected override int PathToID(string path)
100+
{
101+
return Utilities.FindWorldId(path);
102+
}
87103
}

Common/Systems/AutoloadPlayerInWorldSystem.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ private static bool SelectPlayerAndWorld(bool onlyPlayer = false)
145145
Log.Error("No players found after loading players.");
146146
return false;
147147
}
148-
149-
var player = Main.PlayerList.Count > Conf.C.Player ? Main.PlayerList[Conf.C.Player] : null;
148+
int playerId = Utilities.FindPlayerId(Conf.C.Player);
149+
var player = Main.PlayerList.Count > playerId ? Main.PlayerList[playerId] : null;
150150

151151
if (ClientDataJsonHelper.PlayerPath != null && ClientDataJsonHelper.ClientMode != ClientMode.FreshClient)
152152
{
@@ -173,7 +173,8 @@ private static bool SelectPlayerAndWorld(bool onlyPlayer = false)
173173
return false;
174174
}
175175

176-
var world = Main.WorldList.Count > Conf.C.World ? Main.WorldList[Conf.C.World] : null;
176+
int worldId = Utilities.FindWorldId(Conf.C.World);
177+
var world = Main.WorldList.Count > worldId ? Main.WorldList[worldId] : null;
177178

178179
if (ClientDataJsonHelper.WorldPath != null && ClientDataJsonHelper.ClientMode != ClientMode.FreshClient)
179180
{

Common/Systems/Hooks/MainMenu/MainMenuState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ private void AddSingleplayerSection(TooltipPanel tooltipPanel)
123123
{
124124
// Load players and worlds for tooltips
125125
Main.LoadPlayers();
126-
int playerIdx = Conf.C.Player;
126+
int playerIdx = Utilities.FindPlayerId(Conf.C.Player);
127127
if (playerIdx < 0 || playerIdx >= Main.PlayerList.Count) playerIdx = 0;
128128
string playerName = Main.PlayerList.Count > 0 ? Main.PlayerList[playerIdx].Name : "";
129129

130130
Main.LoadWorlds();
131-
int worldIdx = Conf.C.World;
131+
int worldIdx = Utilities.FindWorldId(Conf.C.World);
132132
if (worldIdx < 0 || worldIdx >= Main.WorldList.Count) worldIdx = 0;
133133
string worldName = Main.WorldList.Count > 0 ? Main.WorldList[worldIdx].Name : "";
134134
Log.Info("Loaded and found this many worlds in main menu: " + Main.WorldList.Count);

Helpers/MainMenuActions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static void CreateNewWorld(string desiredName)
7979
{
8080
Main.LoadPlayers();
8181
if (Main.PlayerList.Count == 0) return;
82-
int pIdx = Conf.C.Player;
82+
int pIdx = Utilities.FindPlayerId(Conf.C.Player);
8383
if (pIdx < 0 || pIdx >= Main.PlayerList.Count) pIdx = 0;
8484
Main.SelectPlayer(Main.PlayerList[pIdx]);
8585

Helpers/Utilities.cs

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,90 @@ internal static class Utilities
1313
{
1414
public static int ProcessID => Environment.ProcessId;
1515

16-
/// <summary>
17-
/// Finds the current player ID in the player list.
18-
/// </summary>
19-
/// <returns>The index of the current player in the player list.</returns>
20-
public static string FindCurrentPlayerPath()
21-
{
22-
return Main.ActivePlayerFileData.Path;
23-
}
16+
public static bool _IsPlayersLoaded = false;
17+
18+
public static bool _IsWorldsLoaded = false;
2419

2520
public static PlayerFileData FindPlayer(int i)
2621
{
27-
Main.LoadPlayers();
22+
if (!_IsPlayersLoaded)
23+
{
24+
_IsPlayersLoaded = true;
25+
Main.LoadPlayers();
26+
}
2827
if (i < 0 || i >= Main.PlayerList.Count)
2928
{
3029
return new PlayerFileData()
3130
{
32-
Name = "None"
31+
Name = "None",
32+
_path = ""
3333
};
3434
}
3535
return Main.PlayerList[i];
3636
}
3737

3838
public static PlayerFileData FindPlayer(string path)
3939
{
40-
//Main.LoadPlayers();
40+
if (!_IsPlayersLoaded)
41+
{
42+
_IsPlayersLoaded = true;
43+
Main.LoadPlayers();
44+
}
45+
46+
return Main.PlayerList.FirstOrDefault(p => p.Path == path,
47+
new PlayerFileData() { Name = "None" });
48+
}
4149

42-
return Main.PlayerList.FirstOrDefault(p => p.Path == path,
43-
new PlayerFileData() { Name = "None"});
50+
public static WorldFileData FindWorld(int i)
51+
{
52+
if (!_IsWorldsLoaded)
53+
{
54+
_IsWorldsLoaded = true;
55+
Main.LoadWorlds();
56+
}
57+
58+
if (i < 0 || i >= Main.WorldList.Count)
59+
{
60+
return new WorldFileData()
61+
{
62+
Name = "None",
63+
_path = ""
64+
};
65+
}
66+
return Main.WorldList[i];
4467
}
4568

4669
public static int FindPlayerId(string path)
4770
{
48-
//Main.LoadPlayers();
71+
if (!_IsPlayersLoaded)
72+
{
73+
_IsPlayersLoaded = true;
74+
Main.LoadPlayers();
75+
}
4976

5077
return Main.PlayerList.FindIndex(p => p.Path == path);
5178
}
5279

80+
public static int FindWorldId(string path)
81+
{
82+
if (!_IsWorldsLoaded)
83+
{
84+
_IsWorldsLoaded = true;
85+
Main.LoadWorlds();
86+
}
87+
int index = Main.WorldList.FindIndex(p => p.Path == path);
88+
return index;
89+
}
90+
91+
/// <summary>
92+
/// Finds the current player ID in the player list.
93+
/// </summary>
94+
/// <returns>The index of the current player in the player list.</returns>
95+
public static string FindCurrentPlayerPath()
96+
{
97+
return Main.ActivePlayerFileData.Path;
98+
}
99+
53100
/// <summary>
54101
/// Finds the current world ID in the world list.
55102
/// </summary>

0 commit comments

Comments
 (0)