Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data Saving Progress #8

Open
wants to merge 18 commits into
base: experimental
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ClientSubnautica/ClientManager/Patches/AddMenuButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,27 @@ static void Postfix(string target, MainMenuRightSide __instance)

GameObject.Find("Menu canvas/Panel/MainMenu/RightSide/MultiplayerMenu/SubscriptionSuccess").SetActive(true);
}
catch (SocketException e)
catch (SocketException)
{
GameObject.Find("Menu canvas/Panel/MainMenu/RightSide/MultiplayerMenu/SubscriptionInProgress").SetActive(false);

GameObject.Find("Menu canvas/Panel/MainMenu/RightSide/MultiplayerMenu/SubscriptionError").SetActive(true);
}
catch (FormatException e)
catch (FormatException)
{
GameObject.Find("Menu canvas/Panel/MainMenu/RightSide/MultiplayerMenu/SubscriptionInProgress").SetActive(false);

GameObject.Find("Menu canvas/Panel/MainMenu/RightSide/MultiplayerMenu/SubscriptionError/Text").GetComponent<TextMeshProUGUI>().text = "Invalid address ip";
GameObject.Find("Menu canvas/Panel/MainMenu/RightSide/MultiplayerMenu/SubscriptionError").SetActive(true);
}
catch (IndexOutOfRangeException e)
catch (IndexOutOfRangeException)
{
GameObject.Find("Menu canvas/Panel/MainMenu/RightSide/MultiplayerMenu/SubscriptionInProgress").SetActive(false);

GameObject.Find("Menu canvas/Panel/MainMenu/RightSide/MultiplayerMenu/SubscriptionError/Text").GetComponent<TextMeshProUGUI>().text = "No port specified or wrong ip address format";
GameObject.Find("Menu canvas/Panel/MainMenu/RightSide/MultiplayerMenu/SubscriptionError").SetActive(true);
}
catch (Exception e)
catch (Exception)
{
GameObject.Find("Menu canvas/Panel/MainMenu/RightSide/MultiplayerMenu/SubscriptionInProgress").SetActive(false);

Expand Down
38 changes: 38 additions & 0 deletions ClientSubnautica/ClientManager/Patches/OnSaveGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Net.Sockets;
using System.Threading;
using ClientSubnautica.MultiplayerManager.SendData;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;
using UWE;

namespace ClientSubnautica.ClientManager.Patches
{
class SaveGame
{
[HarmonyPatch(typeof(IngameMenu), "SaveGame")]
public class Patches
{
[HarmonyPostfix]
static bool Prefix(IngameMenu __instance)
{

GameObject mainPanel = __instance.mainPanel;
Button save = __instance.saveButton;

save.onClick.AddListener(SaveButtonClick);

mainPanel.SetActive(true);

return false;
}
private static void SaveButtonClick()
{
Player player = Player.main;
Vector3 position = player.transform.position;

SendOnSaveGame.SendPosition(position);
}
}
}
}
2 changes: 2 additions & 0 deletions ClientSubnautica/ClientSubnautica.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Compile Include="ClientManager\Patches\DisableMailSubscription.cs" />
<Compile Include="ClientManager\Patches\OnPickup.cs" />
<Compile Include="ClientManager\Patches\AddMenuButton.cs" />
<Compile Include="ClientManager\Patches\OnSaveGame.cs" />
<Compile Include="ClientManager\UniqueGuid.cs" />
<Compile Include="ClientManager\Patches\DisablePause.cs" />
<Compile Include="ClientManager\FunctionToClient.cs" />
Expand All @@ -44,6 +45,7 @@
<Compile Include="MultiplayerManager\ReceiveData\ReceiveDataFromServer.cs" />
<Compile Include="MultiplayerManager\SendData\SendOnPickup.cs" />
<Compile Include="MultiplayerManager\SendData\SendOnDrop.cs" />
<Compile Include="MultiplayerManager\SendData\SendOnSaveGame.cs" />
<Compile Include="MultiplayerManager\SendData\SendTimePassed.cs" />
<Compile Include="MultiplayerManager\SendData\SendMyPos.cs" />
<Compile Include="StartMod\LoadMap.cs" />
Expand Down
3 changes: 2 additions & 1 deletion ClientSubnautica/MultiplayerManager/NetworkCMD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class NetworkCMD
{"5", "AllId"},
{"6", "GetTimePassed"},
{"7", "PickupItem"},
{"8", "SpawnBasePiece"}
{"8", "SpawnBasePiece"},
{"9", "SaveGameRequest"}
};

public static string Translate(string idCMD)
Expand Down
78 changes: 78 additions & 0 deletions ClientSubnautica/MultiplayerManager/SendData/SendOnSaveGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Net.Sockets;
using System.Text;
using ClientSubnautica.MultiplayerManager.ReceiveData;
using UnityEngine;

namespace ClientSubnautica.MultiplayerManager.SendData
{
public class SendOnSaveGame
{
public static void SendPosition(Vector3 position)
{
bool saved = SendData(position);
}

private static bool SendData(Vector3 position)
{
NetworkStream ns2 = InitializeConnection.client.GetStream();
try
{
string x = "";
string y = "";
string z = "";
string rotx = "";
string roty = "";
string rotz = "";
string rotw = "";
string rotxTemp = "";
string rotyTemp = "";
string rotzTemp = "";
string rotwTemp = "";
while (true)
{
try
{
Quaternion roTemp = MainCameraControl.main.viewModel.transform.rotation;
rotxTemp = roTemp.x.ToString();
rotyTemp = roTemp.y.ToString();
rotzTemp = roTemp.z.ToString();
rotwTemp = roTemp.w.ToString();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
if (position.x.ToString() != x |
position.y.ToString() != y |
position.z.ToString() != z |
rotxTemp != rotx | rotyTemp != roty |
rotzTemp != rotz | rotwTemp != rotw)
{
rotx = rotxTemp;
roty = rotyTemp;
rotz = rotzTemp;
rotw = rotwTemp;
byte[] msgresponse = Encoding.ASCII.GetBytes("");
Array.Clear(msgresponse, 0, msgresponse.Length);

msgresponse = Encoding.ASCII.GetBytes(NetworkCMD.getIdCMD("SaveGameRequest") + ":" +
position.x + ";" +
position.y + ";" +
position.z + ";" + rotx + ";" +
roty + ";" + rotz + ";" + rotw + "/END/");

ns2.Write(msgresponse, 0, msgresponse.Length);
return true;
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
}
}
}
2 changes: 1 addition & 1 deletion ServerSubnautica/Clients/HandleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void loop()
public void endConnection()
{
lock (Server._lock) Server.list_clients.Remove(id);
Console.WriteLine("Someone deconnected, id: " + id);
Console.WriteLine("Someone disconnected, id: " + id);
client.Client.Shutdown(SocketShutdown.Both);
client.Close();
clientAction.redirectCall(new string[] { id.ToString() }, NetworkCMD.getIdCMD("Disconnected"));
Expand Down
92 changes: 92 additions & 0 deletions ServerSubnautica/Database/DAO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Data.SQLite;
using System.IO;


namespace ServerSubnautica.Database
{
internal class Dao
{
private static SQLiteConnection connection;

public static SQLiteConnection CreateConnection()
{
// Create a new database connection:
connection = new SQLiteConnection("Data Source=database.db; Version = 3; New = True; Compress = True;");
// Open the connection:
try
{
connection.Open();

if (!File.Exists("database.db"))
{
Console.WriteLine("Creating database");
File.Create("database.db");
CreateTable(connection);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
return connection;
}

public static void SaveLocation(string playerId, string lastLocX, string lastLocY, string lastLocZ)
{
SQLiteCommand command = connection.CreateCommand();

command.CommandText =
$"INSERT INTO Players (PlayerId, LastPositionX, LastPositionY, LastPositionZ) VALUES ({playerId}, {lastLocX}, {lastLocY}, {lastLocZ})";
command.ExecuteNonQuery();
}

public static string GetLocation()
{
SQLiteCommand command = connection.CreateCommand();
command.CommandText = "SELECT * FROM Players";

SQLiteDataReader reader = command.ExecuteReader();

while (reader.Read())
{
int readerId = reader.GetInt16(0);
float readerX = reader.GetFloat(1);
float readerY = reader.GetFloat(2);
float readerZ = reader.GetFloat(3);

return $"{readerId};{readerX};{readerY};{readerZ}";
}

return null;
}

private static void CreateTable(SQLiteConnection conn)
{
SQLiteCommand sqliteCmd = conn.CreateCommand();
string createSql = "CREATE TABLE Players (PlayerId INT, LastPositionX REAL, LastPositionY REAL, LastPositionZ REAL)";
string createSql1 = "CREATE TABLE Inventory (PlayerId INT, ItemId INT, Quantity INT, Misc VARCHAR(20))";
sqliteCmd.CommandText = createSql;
sqliteCmd.ExecuteNonQuery();
sqliteCmd.CommandText = createSql1;
sqliteCmd.ExecuteNonQuery();
}

public static void ReadData(SQLiteConnection conn)
{

SQLiteCommand sqLiteCmd = conn.CreateCommand();
sqLiteCmd.CommandText = "SELECT * FROM SampleTable WHERE 1";

SQLiteDataReader sqLiteDataReader = sqLiteCmd.ExecuteReader();

while (sqLiteDataReader.Read())
{
string myReader = sqLiteDataReader.GetString(0);
Console.WriteLine(myReader);
}
conn.Close();
}
}
}
3 changes: 2 additions & 1 deletion ServerSubnautica/NetworkCMD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class NetworkCMD
{"5", "AllId"},
{"6", "GetTimePassed"},
{"7", "PickupItem"},
{"8", "SpawnBasePiece"}
{"8", "SpawnBasePiece"},
{"9", "SaveGameRequest"}
};

public static string Translate(string idCMD)
Expand Down
8 changes: 8 additions & 0 deletions ServerSubnautica/SendData/FunctionManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ClientSubnautica.MultiplayerManager.ReceiveData;
using System;
using ServerSubnautica.Database;

namespace ServerSubnautica
{
Expand Down Expand Up @@ -40,5 +41,12 @@ public void Disconnected(string[] param)
client.broadcast(NetworkCMD.getIdCMD("Disconnected") + ":" + param[0]+"/END/", int.Parse(param[0]));
}

public void SaveGameRequest(string[] param)
{
//client.broadcast(NetworkCMD.getIdCMD("SaveGameRequest") + ":" + param[0] + ":" + param[1] + ":" + param[2] + ":" + param[3] + ":" + param[4] + ":" + param[5] + ":" + param[6]+"/END/", int.Parse(param[0]));
Dao.SaveLocation(param[0], param[1], param[2], param[3]);
Console.WriteLine("id: " + param[0] + " saving at position: " + param[1] + ";" + param[2] + ";" + param[3] + ";" + param[4] + ";" + param[5] + ";" + param[6] + ";" + param[7]);
}

}
}
4 changes: 3 additions & 1 deletion ServerSubnautica/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net;
using System.Net.Sockets;
using System.Threading;
using ServerSubnautica.Database;

class Server
{
Expand All @@ -24,7 +25,8 @@ static void Main(string[] args)
{
Server server = new Server();
configParams = server.loadParam(configPath);


Dao.CreateConnection();

mapName = configParams["MapFolderName"].ToString();
gameInfoPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, mapName, "gameinfo.json");
Expand Down
5 changes: 5 additions & 0 deletions ServerSubnautica/ServerSubnautica.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Data.SQLite" Version="1.0.116" />
</ItemGroup>

<ItemGroup>
<Content Include="bin\Debug\net5.0\database.db" />
</ItemGroup>

</Project>