Skip to content

Commit

Permalink
add import from JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
dfdragon committed Sep 16, 2016
1 parent 1529d8a commit c4b63eb
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 27 deletions.
164 changes: 147 additions & 17 deletions Class_ClientNode.pas
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ TClientNode = class
FisKeepAlive: Integer; FKeepAlive: string;
FisSockBuf: Integer; FSockBuf: string;

procedure initPara;
function GetisRunCMD(): Integer;

procedure SetMemo_log(const Value: TMemo);
Expand Down Expand Up @@ -213,6 +214,7 @@ TClientNode = class
function ModifyXMLNodeValue(Tag: string; TagValue: string): Integer;
function CreateCMDLine(ClientEXEPathName: string): string;
function CreateJSONConfig(): string;
function ReadFromJSONConfig(JSONStr: string): Integer;
function GetHandle(): THandle;
function RunCommand(CommandLine: string): Integer;
function StopCommand(): Integer;
Expand All @@ -232,24 +234,8 @@ implementation

function OpenThread; external kernel32 name 'OpenThread';

constructor TClientNode.Create;
var
NodeGUID: TGUID;
procedure TClientNode.initPara;
begin
inherited;

FMemo_Log:= nil;
FWholeLog:= '';

FCMDThread:= nil;
FXMLNode:= nil;
FCanModifyXML:= True;

CoCreateGuid(NodeGUID);
FID:= GUIDToString(NodeGUID);
FRunState:= '0';
FRemark:= '';

FisJson:= 0; FJson:= '';

FLocalPort:= '9527'; FKCPServerIP:= '127.0.0.1'; FKCPServerPort:= '29900';
Expand Down Expand Up @@ -277,6 +263,27 @@ constructor TClientNode.Create;
FisSockBuf:= 0; FSockBuf:= '';
end;

constructor TClientNode.Create;
var
NodeGUID: TGUID;
begin
inherited;

FMemo_Log:= nil;
FWholeLog:= '';

FCMDThread:= nil;
FXMLNode:= nil;
FCanModifyXML:= True;

CoCreateGuid(NodeGUID);
FID:= GUIDToString(NodeGUID);
FRunState:= '0';
FRemark:= '';

initPara;
end;

destructor TClientNode.Destroy;
begin
FCMDThread.Free;
Expand Down Expand Up @@ -1072,6 +1079,129 @@ function TClientNode.CreateJSONConfig(): string;
end;
end;

function TClientNode.ReadFromJSONConfig(JSONStr: string): Integer;
var
JSONObject: TJSONObject;
StringValue: string;
BooleanValue: Boolean;
IntegerValue: Integer;
ColonPos: Integer;
begin
Result:= 0;
initPara;

JSONObject:= TJSONObject.ParseJSONValue(JSONStr) as TJSONObject;
if JSONObject.TryGetValue<string>('localaddr', StringValue) then
begin
ColonPos:= Pos(':', StringValue);
if ColonPos <> 0 then
LocalPort:= Trim(Copy(StringValue, (ColonPos + 1), (Length(StringValue) - ColonPos)));
end;
if JSONObject.TryGetValue<string>('remoteaddr', StringValue) then
begin
ColonPos:= Pos(':', StringValue);
if ColonPos <> 0 then
begin
KCPServerIP:= Trim(Copy(StringValue, 1, (ColonPos - 1)));
KCPServerPort:= Trim(Copy(StringValue, (ColonPos + 1), (Length(StringValue) - ColonPos)));
end;
end;

if JSONObject.TryGetValue<string>('key', StringValue) then
begin
isKey:= 1;
Key:= StringValue;
end;
if JSONObject.TryGetValue<string>('crypt', StringValue) then
begin
isCrypt:= 1;
Crypt:= StringValue.Trim;
end;
if JSONObject.TryGetValue<Boolean>('nocomp', BooleanValue) then
begin
isNoComp:= Integer(BooleanValue);
end;
if JSONObject.TryGetValue<Integer>('datashard', IntegerValue) then
begin
isDataShard:= 1;
DataShard:= IntegerValue.ToString;
end;
if JSONObject.TryGetValue<Integer>('parityshard', IntegerValue) then
begin
isParityShard:= 1;
ParityShard:= IntegerValue.ToString;
end;
if JSONObject.TryGetValue<Integer>('conn', IntegerValue) then
begin
isConn:= 1;
Conn:= IntegerValue.ToString;
end;
if JSONObject.TryGetValue<Integer>('mtu', IntegerValue) then
begin
isMTU:= 1;
MTU:= IntegerValue.ToString;
end;
if JSONObject.TryGetValue<Integer>('sndwnd', IntegerValue) then
begin
isSndWnd:= 1;
SndWnd:= IntegerValue.ToString;
end;
if JSONObject.TryGetValue<Integer>('rcvwnd', IntegerValue) then
begin
isRcvWnd:= 1;
RcvWnd:= IntegerValue.ToString;
end;
if JSONObject.TryGetValue<Integer>('dscp', IntegerValue) then
begin
isDSCP:= 1;
DSCP:= IntegerValue.ToString;
end;
if JSONObject.TryGetValue<Integer>('autoexpire', IntegerValue) then
begin
isAutoExpire:= 1;
AutoExpire:= IntegerValue.ToString;
end;

if JSONObject.TryGetValue<string>('mode', StringValue) then
begin
isMode:= 1;
Mode:= StringValue.Trim;
end;
if JSONObject.TryGetValue<Integer>('nodelay', IntegerValue) then
begin
isNoDelay:= IntegerValue;
end;
if JSONObject.TryGetValue<Integer>('interval', IntegerValue) then
begin
isInterval:= 1;
Interval:= IntegerValue.ToString;
end;
if JSONObject.TryGetValue<Integer>('resend', IntegerValue) then
begin
isResend:= 1;
Resend:= IntegerValue.ToString;
end;
if JSONObject.TryGetValue<Integer>('nc', IntegerValue) then
begin
isNC:= IntegerValue;
end;

if JSONObject.TryGetValue<Boolean>('acknodelay', BooleanValue) then
begin
isACKNoDelay:= Integer(BooleanValue);
end;
if JSONObject.TryGetValue<Integer>('keepalive', IntegerValue) then
begin
isKeepAlive:= 1;
KeepAlive:= IntegerValue.ToString;
end;
if JSONObject.TryGetValue<Integer>('sockbuf', IntegerValue) then
begin
isSockBuf:= 1;
SockBuf:= IntegerValue.ToString;
end;
end;

function TClientNode.GetHandle(): THandle;
begin
Result:= FCMDThread.CMDHandle;
Expand Down
12 changes: 8 additions & 4 deletions Main.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ object FMain: TFMain
Left = 209
Top = 510
Bitmap = {
494C010104001800C80010001000FFFFFFFFFF10FFFFFFFFFFFFFFFF424D3600
494C010104001800D80010001000FFFFFFFFFF10FFFFFFFFFFFFFFFF424D3600
0000000000003600000028000000400000002000000001002000000000000020
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
Expand Down Expand Up @@ -1447,9 +1447,13 @@ object FMain: TFMain
object N7: TMenuItem
Caption = '-'
end
object Menu_JSON: TMenuItem
Caption = #23548#20986#21040'JSON'#25991#20214'(&J)'
OnClick = Menu_JSONClick
object Menu_ExportToJSON: TMenuItem
Caption = #23548#20986#21040'JSON'#25991#20214'(&E)'
OnClick = Menu_ExportToJSONClick
end
object Menu_ImportFromJSON: TMenuItem
Caption = #20174'JSON'#25991#20214#23548#20837'(&I)'
OnClick = Menu_ImportFromJSONClick
end
end
object OpenDialog_JSON: TOpenDialog
Expand Down
34 changes: 30 additions & 4 deletions Main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface
uses
Winapi.Windows, Winapi.ShellAPI, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls,
Xml.XMLIntf, Xml.XMLDoc, Vcl.Menus, Vcl.Buttons, Vcl.ImgList, PublicVar;
Xml.XMLIntf, Xml.XMLDoc, Vcl.Menus, Vcl.Buttons, Vcl.ImgList, System.JSON, System.IOUtils, PublicVar;

type
TFMain = class(TForm)
Expand Down Expand Up @@ -114,8 +114,9 @@ TFMain = class(TForm)
Label_ConfigFileDir: TLabel;
Btn_FindConfigFileDir: TButton;
OpenDialog_JSON: TOpenDialog;
Menu_JSON: TMenuItem;
Menu_ExportToJSON: TMenuItem;
SaveDialog_JSON: TSaveDialog;
Menu_ImportFromJSON: TMenuItem;
procedure Btn_AddNodeClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Btn_FindClientEXEClick(Sender: TObject);
Expand Down Expand Up @@ -190,8 +191,9 @@ TFMain = class(TForm)
procedure Menu_StartClick(Sender: TObject);
procedure Menu_StopClick(Sender: TObject);
procedure Menu_DeleteClick(Sender: TObject);
procedure Menu_JSONClick(Sender: TObject);
procedure Menu_ExportToJSONClick(Sender: TObject);
procedure Menu_CopyClick(Sender: TObject);
procedure Menu_ImportFromJSONClick(Sender: TObject);
private
{ Private declarations }
procedure WMSYSCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND;
Expand Down Expand Up @@ -1103,6 +1105,7 @@ procedure TFMain.ListView_NodeClick(Sender: TObject);

CheckBox_ConfigFileDir.Enabled:= True;
CheckBox_ConfigFileDirClick(Self);
ComboBox_ModeChange(Self);

Label_Remark.Enabled:= True;
Edit_Remark.Enabled:= True;
Expand Down Expand Up @@ -1243,6 +1246,7 @@ procedure TFMain.Menu_CopyClick(Sender: TObject);
NewRemarkStr:= SouClientNode.Remark + ' COPY';

NewClientNode:= TClientNode.Create;
NewClientNode.MainFormHandle:= Self.Handle;
NewClientNode.XMLDocument_Para:= PublicVar.XMLDocument_Para;
NewClientNode.XMLNode:= NewXMLNode;
NewClientNode.Remark:= NewRemarkStr;
Expand All @@ -1267,7 +1271,7 @@ procedure TFMain.Menu_ExitClick(Sender: TObject);
Close;
end;

procedure TFMain.Menu_JSONClick(Sender: TObject);
procedure TFMain.Menu_ExportToJSONClick(Sender: TObject);
var
ClientNode: TClientNode;
DefaultJSONFileName: string;
Expand Down Expand Up @@ -1317,6 +1321,28 @@ procedure TFMain.Menu_JSONClick(Sender: TObject);
end;
end;

procedure TFMain.Menu_ImportFromJSONClick(Sender: TObject);
var
JSONStr: string;
JSONObject: TJSONObject;
ClientNode: TClientNode;
begin
if Application.MessageBox('导入操作将完全覆盖现有的配置!确定要导入吗?', '提示', MB_YESNO) = MrNo then
Exit;
if not OpenDialog_JSON.Execute then
Exit;
JSONStr:= TFile.ReadAllText(OpenDialog_JSON.FileName);
JSONObject:= TJSONObject.ParseJSONValue(JSONStr) as TJSONObject;
if JSONObject = nil then
begin
Application.MessageBox('您所选择的文件不是有效的JSON格式文件,请重新选择!', '提示', MB_OK);
Exit;
end;
ClientNode:= ListView_Node.Selected.Data;
ClientNode.ReadFromJSONConfig(JSONStr);
ListView_NodeClick(Self);
end;

procedure TFMain.TrayIcon_SysClick(Sender: TObject);
begin
FMain.Visible:= not FMain.Visible;
Expand Down
4 changes: 2 additions & 2 deletions kcptun_gclient.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@
<DCC_DebugInformation>0</DCC_DebugInformation>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_2_Win32)'!=''">
<VerInfo_Release>10</VerInfo_Release>
<VerInfo_Keys>CompanyName=;FileDescription=;FileVersion=1.0.10.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
<VerInfo_Release>11</VerInfo_Release>
<VerInfo_Keys>CompanyName=;FileDescription=;FileVersion=1.0.11.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
<Icon_MainIcon>k.ico</Icon_MainIcon>
<VerInfo_IncludeVerInfo>true</VerInfo_IncludeVerInfo>
<VerInfo_Locale>1033</VerInfo_Locale>
Expand Down
Binary file modified kcptun_gclient.res
Binary file not shown.

0 comments on commit c4b63eb

Please sign in to comment.