Skip to content

Commit

Permalink
handling of systemmessages, load systemmessage data from dat
Browse files Browse the repository at this point in the history
  • Loading branch information
shnok committed Jul 13, 2024
1 parent d43d5ae commit 8c38ae5
Show file tree
Hide file tree
Showing 25 changed files with 5,978 additions and 78 deletions.
16 changes: 16 additions & 0 deletions l2-unity/Assets/Scripts/Database/Model/SystemMessageDat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SystemMessageDat
{
private int _id;
private string _message;
private int _group;
private string _color;

public int Id { get { return _id; } set { _id = value; } }
public string Message { get { return _message; } set { _message = value; } }
public int Group { get { return _group; } set { _group = value; } }
public string Color { get { return _color; } set { _color = value; } }
}

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

84 changes: 84 additions & 0 deletions l2-unity/Assets/Scripts/Database/SystemMessageTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;


public class SystemMessageTable {
private static SystemMessageTable _instance;
public static SystemMessageTable Instance {
get {
if (_instance == null) {
_instance = new SystemMessageTable();
}

return _instance;
}
}

private Dictionary<int, SystemMessageDat> _systemMessages;
public Dictionary<int, SystemMessageDat> SystemMessages { get { return _systemMessages; } }

public void Initialize() {
ReadDatFile();
}

private void ReadDatFile() {
_systemMessages = new Dictionary<int, SystemMessageDat>();
string dataPath = Path.Combine(Application.streamingAssetsPath, "Data/Meta/SystemMsg_Classic-eu.txt");
if (!File.Exists(dataPath)) {
Debug.LogWarning("File not found: " + dataPath);
return;
}

using (StreamReader reader = new StreamReader(dataPath)) {
string line;
while ((line = reader.ReadLine()) != null) {
SystemMessageDat systemMessage = new SystemMessageDat();

string[] keyvals = line.Split('\t');

for (int i = 0; i < keyvals.Length; i++) {
if (!keyvals[i].Contains("=")) {
continue;
}

string[] keyval = keyvals[i].Split("=");
string key = keyval[0];
string value = keyval[1];

switch (key) {
case "id":
systemMessage.Id = int.Parse(value);
break;
case "message":
systemMessage.Message = value.Replace("[", "").Replace("]", "");
break;
case "color":
systemMessage.Color = value;
string r = systemMessage.Color.Substring(0, 2);
string g = systemMessage.Color.Substring(2, 2);
string b = systemMessage.Color.Substring(4, 2);
string a = systemMessage.Color.Substring(6, 2);
systemMessage.Color = b + g + r + "B0";
break;
case "group":
systemMessage.Group = int.Parse(value);
break;
}
}

_systemMessages.TryAdd(systemMessage.Id, systemMessage);
}

Debug.Log($"Successfully imported {_systemMessages.Count} system message(s)");
}
}

public SystemMessageDat GetSystemMessage(int id) {
SystemMessageDat message;
_systemMessages.TryGetValue(id, out message);
return message;
}
}

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

2 changes: 1 addition & 1 deletion l2-unity/Assets/Scripts/Game/Manager/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ private void LoadTables() {
NpcNameTable.Instance.Initialize();
ModelTable.Instance.Initialize();
LogongrpTable.Instance.Initialize();
SystemMessageTable.Instance.Initialize();
}


public void LogIn() {
}

Expand Down
3 changes: 2 additions & 1 deletion l2-unity/Assets/Scripts/Game/Manager/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public void SpawnPlayer(NetworkIdentity identity, PlayerStatus status, PlayerSta

CameraController.Instance.enabled = true;
CameraController.Instance.SetTarget(go);
ChatWindow.Instance.ReceiveChatMessage(new MessageLoggedIn(identity.Name));

//ChatWindow.Instance.ReceiveChatMessage(new MessageLoggedIn(identity.Name));

CharacterInfoWindow.Instance.UpdateValues();

Expand Down
8 changes: 8 additions & 0 deletions l2-unity/Assets/Scripts/Networking/ClientLibrary/Model.meta

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

61 changes: 61 additions & 0 deletions l2-unity/Assets/Scripts/Networking/ClientLibrary/Model/SMParam.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SMParam {
public enum SMParamType {
TYPE_SYSTEM_STRING = 13,
TYPE_PLAYER_NAME = 12,
TYPE_DOOR_NAME = 11,
TYPE_INSTANCE_NAME = 10,
TYPE_ELEMENT_NAME = 9,
// id 8 - same as 3
TYPE_ZONE_NAME = 7,
TYPE_LONG_NUMBER = 6,
TYPE_CASTLE_NAME = 5,
TYPE_SKILL_NAME = 4,
TYPE_ITEM_NAME = 3,
TYPE_NPC_NAME = 2,
TYPE_INT_NUMBER = 1,
TYPE_TEXT = 0
}

private SMParamType _type;
private object _value;

public SMParamType Type { get { return _type; } set { _type = value;} }

public SMParam(SMParamType type, object value) {
_type = type;
_value = value;
}

public SMParam(SMParamType type) {
_type = type;
}

public void SetValue(object value) {
this._value = value;
}

public string GetStringValue() {
return (string)_value;
}

public int GetIntValue() {
return (int)_value;
}

public long GetLongValue() {
return (long)_value;
}

public int[] GetIntArrayValue() {
return (int[])_value;
}

public float[] GetFloatArrayValue() {
return (float[])_value;
}

}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SystemMessageId : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

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

}
}

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

Original file line number Diff line number Diff line change
@@ -1,32 +1,68 @@
using UnityEngine;
using System;

public enum SystemMessageType {
USER_LOGGED_IN,
USER_LOGGED_OFF
}
using static SMParam;

public class SystemMessagePacket : ServerPacket {
private SMParam[] _params;
private int _smId;

private SystemMessageType _type;
public SystemMessage Message { get; private set; }
public SMParam[] Params { get { return _params; } }
public int Id { get { return _smId; } }

public SystemMessagePacket(byte[] d) : base(d) {
Parse();
}

public override void Parse() {
try {
_type = (SystemMessageType)ReadB();

switch (_type) {
case SystemMessageType.USER_LOGGED_IN:
Message = new MessageLoggedIn(ReadS());
break;
case SystemMessageType.USER_LOGGED_OFF:
Message = new MessageLoggedOut(ReadS());
break;
}
_smId = ReadI();

byte paramCount = ReadB();

_params = new SMParam[paramCount];

for (int i = 0; i < paramCount; i++) {

byte paramType = ReadB();

SMParam param = new SMParam((SMParamType) paramType);

switch ((SMParamType)paramType) {
case SMParamType.TYPE_TEXT:
case SMParamType.TYPE_PLAYER_NAME:
param.SetValue(ReadS());
break;
case SMParamType.TYPE_LONG_NUMBER:
param.SetValue(ReadL());
break;
case SMParamType.TYPE_ITEM_NAME:
case SMParamType.TYPE_CASTLE_NAME:
case SMParamType.TYPE_INT_NUMBER:
case SMParamType.TYPE_NPC_NAME:
case SMParamType.TYPE_ELEMENT_NAME:
case SMParamType.TYPE_SYSTEM_STRING:
case SMParamType.TYPE_INSTANCE_NAME:
case SMParamType.TYPE_DOOR_NAME:
param.SetValue(ReadI());
break;
case SMParamType.TYPE_SKILL_NAME:
int[] array = new int[2];
array[0] = ReadI(); // SkillId
array[1] = ReadI(); ; // SkillLevel
param.SetValue(array);
break;
case SMParamType.TYPE_ZONE_NAME:
float[] array2 = new float[3];
array2[0] = ReadF(); // x
array2[1] = ReadF(); // y
array2[2] = ReadF(); // z
param.SetValue(array2);
break;
}

_params[i] = param;
}

} catch(Exception e) {
Debug.LogError(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,17 @@ private void OnMessageReceive(byte[] data) {

private void OnSystemMessageReceive(byte[] data) {
SystemMessagePacket packet = new SystemMessagePacket(data);
SystemMessage message = packet.Message;
_eventProcessor.QueueEvent(() => ChatWindow.Instance.ReceiveChatMessage(message));
SMParam[] smParams = packet.Params;
int messageId = packet.Id;

SystemMessageDat messageData = SystemMessageTable.Instance.GetSystemMessage(messageId);
if(messageData != null) {
SystemMessage systemMessage = new SystemMessage(smParams, messageData);
_eventProcessor.QueueEvent(() => ChatWindow.Instance.ReceiveSystemMessage(systemMessage));
} else {
_eventProcessor.QueueEvent(() => ChatWindow.Instance.ReceiveSystemMessage(new UnhandledMessage()));
}

}

private void OnPlayerInfoReceive(byte[] data) {
Expand Down
4 changes: 2 additions & 2 deletions l2-unity/Assets/Scripts/UI/Game/Chat/ChatTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class ChatTab
{
[SerializeField] string _tabName = "Tab";
[SerializeField] private List<MessageType> _filteredMessages;
//[SerializeField] private List<MessageType> _filteredMessages;
private bool _autoscroll = true;
private ScrollView _scrollView;
private Label _content;
Expand All @@ -15,7 +15,7 @@ public class ChatTab
private VisualElement _tabHeader;
private VisualElement _chatWindowEle;
public string TabName { get { return _tabName; } }
public List<MessageType> FilteredMessages { get { return _filteredMessages; } }
//public List<MessageType> FilteredMessages { get { return _filteredMessages; } }
public Label Content { get { return _content; } }
public VisualElement TabContainer { get { return _tabContainer; } }
public VisualElement TabHeader { get { return _tabHeader; } }
Expand Down
Loading

0 comments on commit 8c38ae5

Please sign in to comment.