Skip to content

Commit

Permalink
Made a lot of stuff static or statically available
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Nov 9, 2023
1 parent 8258289 commit 38cd621
Show file tree
Hide file tree
Showing 22 changed files with 154 additions and 176 deletions.
25 changes: 13 additions & 12 deletions CentrED/CentrED.cs → CentrED/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
using System.Runtime.Loader;
using CentrED.Client;
using CentrED.Server;
using Microsoft.Xna.Framework;

namespace CentrED;

public class CentrED {
public class Application {

static private AssemblyLoadContext _loadContext;
static private string? _rootDir;
Expand Down Expand Up @@ -85,8 +84,9 @@ static private IntPtr ResolveUnmanagedDll(Assembly assembly, string unmanagedDll
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetDllDirectory(string lpPathName);

public static CEDServer? Server;
public static readonly CentrEDClient Client = new();
public static CentrEDGame CEDGame { get; private set; } = null!;
public static CEDServer? CEDServer;
public static readonly CentrEDClient CEDClient = new();

[STAThread]
public static void Main(string[] args)
Expand All @@ -99,14 +99,15 @@ public static void Main(string[] args)
_loadContext = AssemblyLoadContext.Default;
_loadContext.ResolvingUnmanagedDll += ResolveUnmanagedDll;
_loadContext.Resolving += ResolveAssembly;

using Game g = new CentrEDGame();
try {
g.Run();
}
catch (Exception e) {
Console.WriteLine(e.ToString());
File.WriteAllText("Crash.log", e.ToString());

using (CEDGame = new CentrEDGame()) {
try {
CEDGame.Run();
}
catch (Exception e) {
Console.WriteLine(e.ToString());
File.WriteAllText("Crash.log", e.ToString());
}
}
}
}
27 changes: 12 additions & 15 deletions CentrED/CentrEDGame.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Runtime.InteropServices;
using CentrED.Client;
using CentrED.Map;
using CentrED.UI;
using ClassicUO.Utility.Logging;
Expand All @@ -10,12 +9,10 @@ namespace CentrED;

public class CentrEDGame : Game
{
private readonly GraphicsDeviceManager _gdm;
public readonly GraphicsDeviceManager _gdm;

private CentrEDClient _centredClient;
private MapManager _mapManager;
private UIManager _uiManager;
private HuesManager _huesManager;
public MapManager MapManager;
public UIManager UIManager;

public CentrEDGame()
{
Expand All @@ -32,7 +29,7 @@ public CentrEDGame()
Window.ClientSizeChanged += OnWindowResized;
}

protected override unsafe void Initialize()
protected override void Initialize()
{
if (_gdm.GraphicsDevice.Adapter.IsProfileSupported(GraphicsProfile.HiDef))
{
Expand All @@ -44,8 +41,8 @@ protected override unsafe void Initialize()
NativeLibrary.Load(Path.Combine(AppContext.BaseDirectory, "x64", "zlib.dll"));
Log.Start(LogTypes.All);
var background = Content.Load<Texture2D>("background");
_mapManager = new MapManager(_gdm.GraphicsDevice, background);
_uiManager = new UIManager(this, _gdm.GraphicsDevice, _mapManager);
MapManager = new MapManager(_gdm.GraphicsDevice, background);
UIManager = new UIManager(_gdm.GraphicsDevice);

base.Initialize();
}
Expand All @@ -62,9 +59,9 @@ protected override void UnloadContent()

protected override void Update(GameTime gameTime)
{
CentrED.Client.Update();
_uiManager.Update(gameTime, IsActive);
_mapManager.Update(gameTime, IsActive, !_uiManager.CapturingMouse, !_uiManager.CapturingKeyboard);
Application.CEDClient.Update();
UIManager.Update(gameTime, IsActive);
MapManager.Update(gameTime, IsActive, !UIManager.CapturingMouse, !UIManager.CapturingKeyboard);

base.Update(gameTime);
}
Expand All @@ -74,8 +71,8 @@ protected override void Draw(GameTime gameTime)
// if (!IsActive)
// return;

_mapManager.Draw();
_uiManager.Draw(gameTime);
MapManager.Draw();
UIManager.Draw(gameTime);

base.Draw(gameTime);
}
Expand All @@ -84,6 +81,6 @@ protected override void Draw(GameTime gameTime)
private void OnWindowResized(object? sender, EventArgs e) {
GameWindow window = sender as GameWindow;
if (window != null)
_mapManager.OnWindowsResized(window);
MapManager.OnWindowsResized(window);
}
}
2 changes: 1 addition & 1 deletion CentrED/Map/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public MapManager(GraphicsDevice gd, Texture2D background)
_spriteBatch = new SpriteBatch(gd);
_background = background;

Client = CentrED.Client;
Client = Application.CEDClient;
Client.LandTileReplaced += (tile, newId) => {
LandTiles.Find(l => l.LandTile.Equals(tile))?.UpdateId(newId);
};
Expand Down
15 changes: 8 additions & 7 deletions CentrED/Map/RadarMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ClassicUO.Utility;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using static CentrED.Application;

namespace CentrED.Map;

Expand All @@ -13,22 +14,22 @@ public class RadarMap {
public Texture2D Texture => _texture;

private RadarMap(GraphicsDevice gd) {
CentrED.Client.Connected += () => {
_texture = new Texture2D(gd, CentrED.Client.Width, CentrED.Client.Height );
CentrED.Client.Send(new RequestRadarMapPacket());
CEDClient.Connected += () => {
_texture = new Texture2D(gd, CEDClient.Width, CEDClient.Height );
CEDClient.Send(new RequestRadarMapPacket());
};

CentrED.Client.RadarData += RadarData;
CentrED.Client.RadarUpdate += RadarUpdate;
CEDClient.RadarData += RadarData;
CEDClient.RadarUpdate += RadarUpdate;
}

public static void Initialize(GraphicsDevice gd) {
_instance = new RadarMap(gd);
}

private unsafe void RadarData(ushort[] data) {
var width = CentrED.Client.Width;
var height = CentrED.Client.Height;
var width = CEDClient.Width;
var height = CEDClient.Height;
uint[] buffer = System.Buffers.ArrayPool<uint>.Shared.Rent(data.Length);
for (ushort x = 0; x < width; x++) {
for (ushort y = 0; y < height; y++) {
Expand Down
25 changes: 12 additions & 13 deletions CentrED/Tools/DrawTool.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using CentrED.Map;
using CentrED.UI;
using CentrED.UI.Windows;
using ClassicUO.Assets;
using ImGuiNET;
using static CentrED.Application;

namespace CentrED.Tools;

public class DrawTool : Tool {
internal DrawTool(UIManager uiManager) : base(uiManager) { }
public override string Name => "DrawTool";

private bool _pressed;
Expand Down Expand Up @@ -43,12 +42,12 @@ public override void OnMouseEnter(MapObject? o) {
_ => 0
};

var newId = _uiManager._tilesWindow._selectedId;
var newId = CEDGame.UIManager._tilesWindow._selectedId;
if (TilesWindow.IsLandTile(newId)) {
if (o is LandObject lo) {
lo.Visible = false;
var newTile = new LandTile((ushort)newId, lo.Tile.X, lo.Tile.Y, lo.Tile.Z);
_mapManager.GhostLandTiles.Add(new LandObject(_mapManager.Client, newTile));
CEDGame.MapManager.GhostLandTiles.Add(new LandObject(CEDClient, newTile));
}
}
else {
Expand All @@ -66,22 +65,22 @@ public override void OnMouseEnter(MapObject? o) {
tileX,
tileY,
(sbyte)newZ,
(ushort)(_withHue ? _uiManager._huesWindow.SelectedId + 1 : 0));
_mapManager.GhostStaticTiles.Add(new StaticObject(newTile));
(ushort)(_withHue ? CEDGame.UIManager._huesWindow.SelectedId + 1 : 0));
CEDGame.MapManager.GhostStaticTiles.Add(new StaticObject(newTile));
}
}

public override void OnMouseLeave(MapObject? o) {
if (TilesWindow.IsLandTile(_uiManager._tilesWindow._selectedId)) {
if (TilesWindow.IsLandTile(CEDGame.UIManager._tilesWindow._selectedId)) {
if (o is LandObject lo) {
lo.Visible = true;
_mapManager.GhostLandTiles.Clear();
CEDGame.MapManager.GhostLandTiles.Clear();
}
}
else {
if (o != null)
o.Visible = true;
_mapManager.GhostStaticTiles.Clear();
CEDGame.MapManager.GhostStaticTiles.Clear();
}
}

Expand All @@ -93,13 +92,13 @@ public override void OnMousePressed(MapObject? o) {

public override void OnMouseReleased(MapObject? o) {
if (_pressed && o == _focusObject) {
var newId = _uiManager._tilesWindow._selectedId;
var newId = CEDGame.UIManager._tilesWindow._selectedId;
if (TilesWindow.IsLandTile(newId) && o is LandObject lo) {
lo.LandTile.Id = (ushort)_uiManager._tilesWindow._selectedId;
lo.LandTile.Id = (ushort)CEDGame.UIManager._tilesWindow._selectedId;
}
else {
var newTile = _mapManager.GhostStaticTiles[0].StaticTile;
_mapManager.Client.Add(newTile);
var newTile = CEDGame.MapManager.GhostStaticTiles[0].StaticTile;
CEDGame.MapManager.Client.Add(newTile);
}
}
_pressed = false;
Expand Down
12 changes: 5 additions & 7 deletions CentrED/Tools/ElevateTool.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using CentrED.Map;
using CentrED.UI;
using ImGuiNET;
using static CentrED.Application;

namespace CentrED.Tools;

public class ElevateTool : Tool {
internal ElevateTool(UIManager uiManager) : base(uiManager) { }

[Flags]
enum ZMode {
INC = 0,
Expand Down Expand Up @@ -44,22 +42,22 @@ public override void OnMouseEnter(MapObject? o) {
var tile = so.StaticTile;
so.Alpha = 0.3f;
var newTile = new StaticTile(tile.Id, tile.X, tile.Y, NewZ(tile), tile.Hue);
_mapManager.GhostStaticTiles.Add(new StaticObject(newTile));
CEDGame.MapManager.GhostStaticTiles.Add(new StaticObject(newTile));
} else if (o is LandObject lo) {
var tile = lo.LandTile;
lo.Visible = false;
var newTile = new LandTile(tile.Id, tile.X, tile.Y, NewZ(tile));
_mapManager.GhostLandTiles.Add(new LandObject(_mapManager.Client, newTile));
CEDGame.MapManager.GhostLandTiles.Add(new LandObject(CEDGame.MapManager.Client, newTile));
}
}

public override void OnMouseLeave(MapObject? o) {
if (o is StaticObject so) {
so.Alpha = 1f;
_mapManager.GhostStaticTiles.Clear();
CEDGame.MapManager.GhostStaticTiles.Clear();
} else if (o is LandObject lo) {
lo.Visible = true;
_mapManager.GhostLandTiles.Clear();
CEDGame.MapManager.GhostLandTiles.Clear();
}
}

Expand Down
11 changes: 5 additions & 6 deletions CentrED/Tools/HueTool.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using CentrED.Map;
using CentrED.UI;
using static CentrED.Application;

namespace CentrED.Tools;

public class HueTool : Tool {
internal HueTool(UIManager uiManager) : base(uiManager) { }

public override string Name => "HueTool";

private bool _pressed;
private StaticObject _focusObject;

public override void OnActivated(MapObject? o) {
_uiManager._huesWindow.Show = true;
CEDGame.UIManager._huesWindow.Show = true;
}

public override void OnMouseEnter(MapObject? o) {
if (o is StaticObject so) {
so.Hue = (ushort)_uiManager._huesWindow.SelectedId;
so.Hue = (ushort)CEDGame.UIManager._huesWindow.SelectedId;
}
}

Expand All @@ -36,8 +35,8 @@ public override void OnMousePressed(MapObject? o) {

public override void OnMouseReleased(MapObject? o) {
if (_pressed && o is StaticObject so && so == _focusObject) {
if(_uiManager._huesWindow.SelectedId != -1)
so.StaticTile.Hue = (ushort)_uiManager._huesWindow.SelectedId;
if(CEDGame.UIManager._huesWindow.SelectedId != -1)
so.StaticTile.Hue = (ushort)CEDGame.UIManager._huesWindow.SelectedId;
}
_pressed = false;
}
Expand Down
8 changes: 3 additions & 5 deletions CentrED/Tools/MoveTool.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using CentrED.Map;
using CentrED.UI;
using ImGuiNET;
using static CentrED.Application;

namespace CentrED.Tools;

public class MoveTool : Tool {
internal MoveTool(UIManager uiManager) : base(uiManager) { }

public override string Name => "MoveTool";

private int _xDelta;
Expand All @@ -33,14 +31,14 @@ public override void OnMouseEnter(MapObject? o) {
so.StaticTile.Z,
so.StaticTile.Hue
);
_mapManager.GhostStaticTiles.Add(new StaticObject(newTile));
CEDGame.MapManager.GhostStaticTiles.Add(new StaticObject(newTile));
}
}

public override void OnMouseLeave(MapObject? o) {
if (o is StaticObject so) {
so.Alpha = 1f;
_mapManager.GhostStaticTiles.Clear();
CEDGame.MapManager.GhostStaticTiles.Clear();
}
}

Expand Down
5 changes: 2 additions & 3 deletions CentrED/Tools/RemoveTool.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using CentrED.Map;
using CentrED.UI;
using static CentrED.Application;

namespace CentrED.Tools;

public class RemoveTool : Tool {
internal RemoveTool(UIManager uiManager) : base(uiManager) { }
public override string Name => "RemoveTool";

private bool _pressed;
Expand All @@ -31,7 +30,7 @@ public override void OnMousePressed(MapObject? o) {

public override void OnMouseReleased(MapObject? o) {
if (_pressed && o is StaticObject so && so == _focusObject) {
_mapManager.Client.Remove(_focusObject.StaticTile);
CEDClient.Remove(_focusObject.StaticTile);
}
_pressed = false;
}
Expand Down
Loading

0 comments on commit 38cd621

Please sign in to comment.