//⚠ aardio文件加解密 2.1 【光庆】
//光庆 http://chengxu.online
//资料： https://blog.csdn.net/sdlgq/article/details/121899182

import win.ui;
import win.dlg.message;
import fsys.dlg
import fonts.fontAwesome;
//import godking;
import godking.message;
/*DSG{{*/
var winform = win.form(cls="AFORM";text="aardio文件加解密 2.1 (光庆)";right=727;bottom=591;acceptfiles=1;border="none";max=false;parent=...;style=0)
winform.add(
bkplus={cls="bkplus";text='      \uF13E    aardio文件加解密 2.1 (光庆)';left=0;top=0;right=728;bottom=56;align="left";bgcolor=1151245;color=15793151;font=LOGFONT(h=-16;name='FontAwesome');z=16};
checkbox={cls="checkbox";text="覆盖自身";left=70;top=510;right=160;bottom=529;font=LOGFONT(h=-14);z=5};
checkbox2={cls="checkbox";text="附加可解密代码";left=160;top=510;right=290;bottom=529;font=LOGFONT(h=-14);z=15};
checkbox3={cls="checkbox";text="生成提示文件";left=290;top=510;right=410;bottom=529;font=LOGFONT(h=-14);z=18};
edit={cls="edit";left=16;top=536;right=395;bottom=566;bgcolor=16777215;border=1;tabstop=1;z=2};
edit2={cls="edit";left=16;top=160;right=360;bottom=496;border=1;multiline=1;readonly=1;vscroll=1;z=6};
edit3={cls="edit";left=368;top=160;right=712;bottom=496;border=1;multiline=1;readonly=1;vscroll=1;z=7};
editpath={cls="edit";left=16;top=96;right=402;bottom=126;bgcolor=16777215;border=1;readonly=1;tabstop=1;z=1};
plusjiami={cls="plus";text='\uF023 加密';left=496;top=528;right=600;bottom=569;bgcolor=49152;border={radius=20};font=LOGFONT(h=-16;name='FontAwesome');notify=1;z=11};
plusjiemi={cls="plus";text='\uF0F6 解密';left=608;top=528;right=712;bottom=569;bgcolor=49407;border={radius=20};font=LOGFONT(h=-16;name='FontAwesome');notify=1;z=14};
plusliulan={cls="plus";text='\uF115 浏览';left=408;top=96;right=484;bottom=126;bgcolor=16744448;border={radius=15};color=16777215;font=LOGFONT(h=-16;name='FontAwesome');notify=1;z=13};
plusliulan2={cls="plus";text='\uF115 浏览';left=408;top=536;right=484;bottom=566;bgcolor=16744448;border={radius=15};color=16777215;font=LOGFONT(h=-16;name='FontAwesome');notify=1;z=12};
static={cls="static";text="1、加密后的文件仍可被aardio调用。2、加密时会把$包含的文件数据直接编译进加密文件。";left=64;top=72;right=720;bottom=88;color=8421504;transparent=1;z=17};
static2={cls="static";text="源文件";left=16;top=72;right=157;bottom=95;transparent=1;z=3};
static3={cls="static";text="保存到";left=16;top=512;right=69;bottom=531;font=LOGFONT(h=-14);transparent=1;z=4};
static4={cls="static";text="代码部分";left=16;top=136;right=157;bottom=159;transparent=1;z=8};
static5={cls="static";text="提示部分";left=368;top=136;right=509;bottom=159;transparent=1;z=9};
static6={cls="static";left=496;top=88;right=712;bottom=134;align="center";center=1;color=8388736;font=LOGFONT(h=-35;name='FontAwesome');transparent=1;z=10}
)
/*}}*/

winform.plusjiemi.disabledText = {'\uF254';'\uF251';'\uF252';'\uF253';'\uF250';text='\uF00D'}
winform.plusjiami.disabledText = {'\uF254';'\uF251';'\uF252';'\uF253';'\uF250';text='\uF00D'}
import win.ui.simpleWindow
var tt = win.ui.simpleWindow(winform,20,40,30,50,0xFF008CBA,0xFF00B4F4);
var msg =  godking.message(winform);
var code,itls;
var chuli = function(str){
		winform.plusjiemi.disabledText = {'\uF254';'\uF251';'\uF252';'\uF253';'\uF250';text='\uF00D'}
		winform.plusjiami.disabledText = {'\uF254';'\uF251';'\uF252';'\uF253';'\uF250';text='\uF00D'}
		if !..io.exist(str) return ;
		winform.editpath.text = str;
		code = ..string.load(winform.editpath.text);
		var kejiemi = ..string.endWith(code,'\1\2\3\4'); 
		var index = ..string.find(code,"\/\**?intellisense\(");
		if index {
			itls = ..string.right(code,#code-index+1);
		}
		winform.edit2.text = code;
		winform.edit3.text = itls;
		var p = ..io.splitpath(str)
		if p.name = "code"	winform.edit.text = ..io.joinpath(p.dir,"user.aardio");
		else winform.edit.text = "";
		if  ..string.startWith(code,"=aardio"){
			if kejiemi {
				winform.static6.text = '\uF023 可解密'
				winform.plusjiemi.disabledText = null;
			} else winform.static6.text = '\uF023 已加密'
			winform.static6.color = 0x0000FF;
		} else {
			winform.plusjiami.disabledText = null;
			winform.static6.text = '\uF0F6 未加密'
			winform.static6.color = 0x259B24;
		}		
		winform.static6.redraw()
		
}

winform.wndproc = function(hwnd,message,wParam,lParam){
	select(message) {//判断消息类型
		case 0x233/*_WM_DROPFILES*/ { 
			var str = win.getDropFile(wParam )[1]; 
			if str chuli(str);
		} 
	}
} 

winform.plusjiami.oncommand = function(id,event){
	if( winform.editpath.text == "" or !#code){
		msg.frown("请选择要加密的文件！",1000)
		return ;
	}
	if  ..string.startWith(code,"=aardio"){
		msg.err("该文件为加密文件！不能再次加密",1000)
		return ; 
	}
	var tfile = winform.edit.text;
	if winform.checkbox.checked tfile=winform.editpath.text;
	if !#tfile {
		msg.frown("还没有选择保存的文件！",1000)
		return ;
	}
	if ..string.lower(..io.splitpath(tfile).name)=="code" return msg.err("不能加密为code文件！",1000); 
	// 处理加密文本
	var str = dumpcode(assert(loadcode(code)));
	if winform.checkbox2.checked {
		var pass = msg.input("请输入加密密码：",true);
		if !#pass return ; 
		import crypt.aes
		var aes = crypt.aes()
		aes.setPassword(pass)
		var a = aes.encrypt(pass++code++pass);
		//..string.save(tfile, str ++ a ++ ..string.right("0000000000"+ #a,10))
		..string.save(tfile, str ++ a ++ tostring(..raw.buffer({int v=#a}))++'\1\2\3\4')
	} else {
		..string.save(tfile, str)
	}	
	// 处理智能提示文件
	if winform.checkbox3.checked and #itls{
		var tfile = ..io.splitpath(tfile);
		tfile = tfile.drive++tfile.path++tfile.name++"_intellisense"++tfile.ext;
		string.save(tfile,itls);
	}
	msg.ok("文件加密完成！",1000);
}

winform.plusliulan.oncommand = function(id,event){
	var str = fsys.dlg.open("aardio文件|*.aardio||","请选择aardio代码文件")
	if !str return ; 
	if str chuli(str);
}

winform.plusliulan2.oncommand = function(id,event){
	var path = fsys.dlg.save("aardio文件|*.aardio||","保存编译后的二进制aardio文件")
	if(path) winform.edit.text = path;
}

winform.plusjiemi.oncommand = function(id,event){
	
	if (winform.editpath.text == "" or !#code){
		msg.frown("请选择源文件！",1000)
		return ;
	}
	if !..string.startWith(code,"=aardio"){
		msg.err("该文件不是加密文件！不能解密",1000)
		return ; 
	}
	var tfile = winform.edit.text;
	if winform.checkbox.checked tfile=winform.editpath.text;
	if !#tfile {
		msg.frown("还没有选择保存的文件！",1000)
		return ;
	}
	
	if !..string.endWith(code,'\1\2\3\4') return msg.err("该文件不是含源代码的加密文件，不能解密！",1000); 
	
	var length = string.slice(code,-8,-5);
	length = ..raw.convert(length,{int v})["v"];
	if !length or length<1 or length>#code {
		msg.err("加密信息不正确，不能解密！",1000)
		return ; 		
	}
	
	var pass = msg.input("请输入解密密码：",true);
	if !#pass return ; 
	
	var txt = ..string.slice(code,-length-8,-9);
	import crypt.aes
	var aes = crypt.aes()
	aes.setPassword(pass)
	var a = aes.decrypt(txt);
	if !#a return msg.err("解密失败！",1000);

	if !..string.startWith(a,pass) or !..string.endWith(a,pass) return msg.err("密码不正确！",1000); 
	a = ..string.slice(a,#pass+1,#a-#pass);
	string.save(tfile,a);
	msg.ok("文件解密完成！");	
}

winform.show(true) 
win.loopMessage( winform );