Data server
The data server is an example of a database server implementation (Wonderland/SimpleNet/ServerRel/ServerDataFile
). This is simple and uses folders and files to store game data. The config file name is serverdata_config.json
.
{
"FPS": 60,
"Port": 5880,
"Password": "****",
"DataPath": "Games"
}
DataPath
is the folder where games will be stored.
You can write your own to store in a bdd or other if you want.
The data for get or set is set by Server
. You can store anything you want because the packet data is stored directly. Server
is just the man in the middle, used to hide the data server to clients. Packets are direcly sended to data server without modification.
Get data
var result = await NetManager.GetData<string>(string gameId, string varId);
// alias of
var response = await NetManager.Request(Packet.Create(SimpleNetCmds.GetData, gameId, varId));
if (response != null) {
var success = response.Read<bool>();
if (success) {
T val = default(T);
response.Read(ref val);
return val;
}
}
return default(T);
To save data, use the gameId and gen en password at the game ID creation. After the first save, this password is associated with this game data and it is required to set data.
Set data
bool success = await NetManager.SetData(string gameId, string psw, string varId, T value);
if (!success) {
Debug.LogError("Fail");
}
// alias of
var response = await NetManager.Request(Packet.Create(SimpleNetCmds.SetData, gameId, psw, varId, value), _cloud.DefaultNetwork);
if (response != null) {
var success = response.Read<bool>();
if (success) {
return true;
}
}
return false;
It is recommended to allow only one player to save the current data. However, a trick can be used
AnExistingVariable.IsEmitter
.
Create your own
You can inherit ServerData
and override SetData
and GetData
methods. This method receives the packet sent with GetData
/SetData
.
ServerDataFile
using System.IO;
using System.Threading.Tasks;
using EODE.Wonderland.Networking;
using UnityEngine;
/* Default config file
{
"FPS": 60,
"Port": 5880,
"Password": "****",
"DataPath": "Games"
}
*/
namespace EODE.Wonderland.SimpleNet {
public class ServerDataFile : ServerData {
public string FileName = "serverdata_config.json";
class Config {
public int FPS = 60;
public int Port = 5880;
public string Password = "****";
public string DataPath = "Games";
}
string DataPath = "";
void Awake() {
var path = Path.Combine(Application.dataPath, "..", FileName);
if (!File.Exists(path)) {
System.Console.Error.Write("Config not found\n");
System.Console.Error.Write("Required file : " + path + "\n");
}
try {
var conf = JsonUtility.FromJson<Config>(File.ReadAllText(path));
LaunchServer(conf);
}
catch (System.Exception e) {
System.Console.Error.Write(e);
System.Console.Error.Write("\n");
}
}
void LaunchServer(Config conf) {
FPS = conf.FPS;
Port = conf.Port;
Password = conf.Password;
DataPath = Path.Combine(Application.dataPath, "..", conf.DataPath);
if (!Directory.Exists(DataPath)) {
var info = Directory.CreateDirectory(DataPath);
if (!info.Exists) {
System.Console.Error.WriteLine("Cannot create directory " + DataPath);
Application.Quit();
return;
}
}
Init();
}
public override Task<byte[]> GetData(Packet packet) {
var gameId = packet.Read<string>();
if (!string.IsNullOrEmpty(gameId)) {
var varId = packet.Read<string>();
if (!string.IsNullOrEmpty(varId)) {
var filePath = Path.Combine(DataPath, gameId, varId);
if (!File.Exists(Path.Combine(filePath))) {
return null;
}
return Task.FromResult(File.ReadAllBytes(filePath));
}
}
return null;
}
public override Task<bool> SetData(Packet packet) {
var gameId = packet.Read<string>();
var dataPsw = packet.Read<string>();
if (!string.IsNullOrEmpty(gameId)) {
// gets and creates directory if not exists
var gameDirPath = Path.Combine(DataPath, gameId);
// password file path
var pswPath = Path.Combine(gameDirPath, "password");
if (!Directory.Exists(gameDirPath)) {
var gdir = Directory.CreateDirectory(gameDirPath);
if (!gdir.Exists) {
System.Console.Error.WriteLine("Cannot create directory " + gdir);
return Task.FromResult(false);
}
// save password
File.WriteAllText(pswPath, dataPsw);
}
else {
var psw = File.ReadAllText(pswPath);
if (psw != dataPsw) return Task.FromResult(false);
}
var varId = packet.Read<string>();
if (!string.IsNullOrEmpty(varId)) {
var filePath = Path.Combine(gameDirPath, varId);
File.WriteAllBytes(filePath, packet.DataRest);
System.Console.WriteLine("Data " + gameId + "#" + varId + " updated");
return Task.FromResult(true);
}
}
return Task.FromResult(false);
}
}
}