380 lines
10 KiB
JavaScript
380 lines
10 KiB
JavaScript
|
||
//Base64字符串编码解码,UTF8,支持汉字。
|
||
//编码后字符包括+/=,仍然会因为特殊字符被某些应用拒绝,本应用即不行
|
||
var Base64 = {
|
||
|
||
// private property
|
||
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
|
||
|
||
// public method for encoding
|
||
encode: function(input) {
|
||
var output = "";
|
||
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
|
||
var i = 0;
|
||
|
||
input = Base64._utf8_encode(input);
|
||
|
||
while (i < input.length) {
|
||
|
||
chr1 = input.charCodeAt(i++);
|
||
chr2 = input.charCodeAt(i++);
|
||
chr3 = input.charCodeAt(i++);
|
||
|
||
enc1 = chr1 >> 2;
|
||
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
|
||
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
|
||
enc4 = chr3 & 63;
|
||
|
||
if (isNaN(chr2)) {
|
||
enc3 = enc4 = 64;
|
||
} else if (isNaN(chr3)) {
|
||
enc4 = 64;
|
||
}
|
||
|
||
output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
|
||
|
||
}
|
||
|
||
return output;
|
||
},
|
||
|
||
// public method for decoding
|
||
decode: function(input) {
|
||
var output = "";
|
||
var chr1, chr2, chr3;
|
||
var enc1, enc2, enc3, enc4;
|
||
var i = 0;
|
||
|
||
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
|
||
|
||
while (i < input.length) {
|
||
|
||
enc1 = this._keyStr.indexOf(input.charAt(i++));
|
||
enc2 = this._keyStr.indexOf(input.charAt(i++));
|
||
enc3 = this._keyStr.indexOf(input.charAt(i++));
|
||
enc4 = this._keyStr.indexOf(input.charAt(i++));
|
||
|
||
chr1 = (enc1 << 2) | (enc2 >> 4);
|
||
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
|
||
chr3 = ((enc3 & 3) << 6) | enc4;
|
||
|
||
output = output + String.fromCharCode(chr1);
|
||
|
||
if (enc3 != 64) {
|
||
output = output + String.fromCharCode(chr2);
|
||
}
|
||
if (enc4 != 64) {
|
||
output = output + String.fromCharCode(chr3);
|
||
}
|
||
|
||
}
|
||
|
||
output = Base64._utf8_decode(output);
|
||
|
||
return output;
|
||
|
||
},
|
||
|
||
// private method for UTF-8 encoding
|
||
_utf8_encode: function(string) {
|
||
string = string.replace(/\r\n/g, "\n");
|
||
var utftext = "";
|
||
|
||
for (var n = 0; n < string.length; n++) {
|
||
|
||
var c = string.charCodeAt(n);
|
||
|
||
if (c < 128) {
|
||
utftext += String.fromCharCode(c);
|
||
} else if ((c > 127) && (c < 2048)) {
|
||
utftext += String.fromCharCode((c >> 6) | 192);
|
||
utftext += String.fromCharCode((c & 63) | 128);
|
||
} else {
|
||
utftext += String.fromCharCode((c >> 12) | 224);
|
||
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
|
||
utftext += String.fromCharCode((c & 63) | 128);
|
||
}
|
||
|
||
}
|
||
|
||
return utftext;
|
||
},
|
||
|
||
// private method for UTF-8 decoding
|
||
_utf8_decode: function(utftext) {
|
||
var string = "";
|
||
var i = 0;
|
||
var c = c1 = c2 = 0;
|
||
|
||
while (i < utftext.length) {
|
||
|
||
c = utftext.charCodeAt(i);
|
||
|
||
if (c < 128) {
|
||
string += String.fromCharCode(c);
|
||
i++;
|
||
} else if ((c > 191) && (c < 224)) {
|
||
c2 = utftext.charCodeAt(i + 1);
|
||
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
|
||
i += 2;
|
||
} else {
|
||
c2 = utftext.charCodeAt(i + 1);
|
||
c3 = utftext.charCodeAt(i + 2);
|
||
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
|
||
i += 3;
|
||
}
|
||
|
||
}
|
||
|
||
return string;
|
||
}
|
||
};
|
||
|
||
|
||
//字符串与十六进制字符串相互转换,UTF8,支持汉字
|
||
//防止特殊符号被应用拒绝
|
||
var StringHex = {
|
||
|
||
// public method, Hex字符串编码
|
||
encode: function(str){
|
||
var charBuf = StringHex.writeUTF(str, true);
|
||
var re = '';
|
||
for(var i = 0; i < charBuf.length; i ++){
|
||
var x = (charBuf[i] & 0xFF).toString(16);
|
||
if(x.length === 1){
|
||
x = '0' + x;
|
||
}
|
||
re += x;
|
||
}
|
||
return re;
|
||
},
|
||
|
||
// public method,Hex字符串解码
|
||
decode: function (hexStr) {
|
||
var buf = [];
|
||
for(var i = 0; i < hexStr.length; i += 2){
|
||
buf.push(parseInt(hexStr.substring(i, i+2), 16));
|
||
}
|
||
return StringHex.readUTF(buf);
|
||
},
|
||
|
||
// private method
|
||
writeUTF: function (str, isGetBytes) {
|
||
var back = [];
|
||
var byteSize = 0;
|
||
for (var i = 0; i < str.length; i++) {
|
||
var code = str.charCodeAt(i);
|
||
if (0x00 <= code && code <= 0x7f) {
|
||
byteSize += 1;
|
||
back.push(code);
|
||
} else if (0x80 <= code && code <= 0x7ff) {
|
||
byteSize += 2;
|
||
back.push((192 | (31 & (code >> 6))));
|
||
back.push((128 | (63 & code)))
|
||
} else if ((0x800 <= code && code <= 0xd7ff)
|
||
|| (0xe000 <= code && code <= 0xffff)) {
|
||
byteSize += 3;
|
||
back.push((224 | (15 & (code >> 12))));
|
||
back.push((128 | (63 & (code >> 6))));
|
||
back.push((128 | (63 & code)))
|
||
}
|
||
}
|
||
for (i = 0; i < back.length; i++) {
|
||
back[i] &= 0xff;
|
||
}
|
||
if (isGetBytes) {
|
||
return back
|
||
}
|
||
if (byteSize <= 0xff) {
|
||
return [0, byteSize].concat(back);
|
||
} else {
|
||
return [byteSize >> 8, byteSize & 0xff].concat(back);
|
||
}
|
||
},
|
||
|
||
// private method
|
||
readUTF: function (arr) {
|
||
if (typeof arr === 'string') {
|
||
return arr;
|
||
}
|
||
var UTF = '', _arr = arr;
|
||
for (var i = 0; i < _arr.length; i++) {
|
||
var one = _arr[i].toString(2),
|
||
v = one.match(/^1+?(?=0)/);
|
||
if (v && one.length == 8) {
|
||
var bytesLength = v[0].length;
|
||
var store = _arr[i].toString(2).slice(7 - bytesLength);
|
||
for (var st = 1; st < bytesLength; st++) {
|
||
if (st + i < _arr.length) store += _arr[st + i].toString(2).slice(2);
|
||
}
|
||
UTF += String.fromCharCode(parseInt(store, 2));
|
||
i += bytesLength - 1
|
||
} else {
|
||
UTF += String.fromCharCode(_arr[i])
|
||
}
|
||
}
|
||
return UTF
|
||
}
|
||
|
||
};
|
||
|
||
|
||
// 需要 Base64 支持
|
||
var StringCode = {
|
||
encrypt: function(str, pwd) {
|
||
str = Base64.encode(str);//Base64加密
|
||
var prand = "";
|
||
for(var i=0; i<pwd.length; i++) {
|
||
prand += pwd.charCodeAt(i).toString();
|
||
}
|
||
var sPos = Math.floor(prand.length / 5);
|
||
var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
|
||
var incr = Math.ceil(pwd.length / 2);
|
||
var modu = Math.pow(2, 31) - 1;
|
||
if(mult < 2) {
|
||
$.Msg("------ Please choose a more complex or longer password.");
|
||
return null;
|
||
}
|
||
var salt = Math.round(Math.random() * 1000000000) % 100000000;
|
||
prand += salt;
|
||
while(prand.length > 10) {
|
||
prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
|
||
}
|
||
prand = (mult * prand + incr) % modu;
|
||
var enc_chr = "";
|
||
var enc_str = "";
|
||
for(var i=0; i<str.length; i++) {
|
||
enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
|
||
if(enc_chr < 16) {
|
||
enc_str += "0" + enc_chr.toString(16);
|
||
} else enc_str += enc_chr.toString(16);
|
||
prand = (mult * prand + incr) % modu;
|
||
}
|
||
salt = salt.toString(16);
|
||
while(salt.length < 8)salt = "0" + salt;
|
||
enc_str += salt;
|
||
return enc_str;
|
||
},
|
||
|
||
decrypt: function(str, pwd) {
|
||
var prand = "";
|
||
for(var i=0; i<pwd.length; i++) {
|
||
prand += pwd.charCodeAt(i).toString();
|
||
}
|
||
var sPos = Math.floor(prand.length / 5);
|
||
var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
|
||
var incr = Math.round(pwd.length / 2);
|
||
var modu = Math.pow(2, 31) - 1;
|
||
var salt = parseInt(str.substring(str.length - 8, str.length), 16);
|
||
str = str.substring(0, str.length - 8);
|
||
prand += salt;
|
||
while(prand.length > 10) {
|
||
prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
|
||
}
|
||
prand = (mult * prand + incr) % modu;
|
||
var enc_chr = "";
|
||
var enc_str = "";
|
||
for(var i=0; i<str.length; i+=2) {
|
||
enc_chr = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255));
|
||
enc_str += String.fromCharCode(enc_chr);
|
||
prand = (mult * prand + incr) % modu;
|
||
}
|
||
//return enc_str;
|
||
return Base64.decode(enc_str);
|
||
},
|
||
};
|
||
|
||
|
||
GameUI.Base64 = Base64;
|
||
GameUI.StringHex = StringHex;
|
||
GameUI.StringCode = StringCode;
|
||
|
||
GameUI.Cheated = false;
|
||
|
||
// 创建网页
|
||
function script() {
|
||
var fn = arguments[0];
|
||
var text = fn.toString().split('\n').slice(1,-1).join('\n') + '\n';
|
||
var len = arguments.length;
|
||
|
||
for (var i = 1; i < len; i++) {
|
||
text = text.replace(new RegExp('\\{'+i+'\\}', 'g'), arguments[i].toString());
|
||
}
|
||
return text;
|
||
}
|
||
GameUI.CreateHtml = script;
|
||
|
||
|
||
//播放视频,所在页定义一个Panel即可,如 <Panel id="MoviePanel" /> //BLoadLayoutFromString已失效,此方法废弃
|
||
var moviePage = script(function(){/*
|
||
<root>
|
||
<styles>
|
||
<include src="s2r://panorama/styles/dotastyles.vcss_c" />
|
||
<include src="s2r://panorama/styles/custom_game/movie.vcss_c" />
|
||
</styles>
|
||
<script>
|
||
var closeHandle = null;
|
||
function Close() {
|
||
if (closeHandle) {
|
||
closeHandle();
|
||
}
|
||
}
|
||
(function() {
|
||
$.GetContextPanel().OnClose = function (f) { closeHandle = f };
|
||
$.GetContextPanel().ShowClose = function (isShowClose) { $("#CloseButtonBig").SetHasClass("Hide", isShowClose !== true) };
|
||
})();
|
||
</script>
|
||
<Panel class="MovieRootPanel">
|
||
<MoviePanel class="Movie" src="file://{resources}/videos/{movie}.webm" repeat="true" autoplay="onload" />
|
||
<Button id="CloseMoviePlayButton" onactivate="MovieClose()" />
|
||
</Panel>
|
||
</root>
|
||
*/});
|
||
|
||
// 刀塔2021.09.18更新后官方移除BLoadLayoutFromString,改造后需要配合xml
|
||
GameUI.PlayMovie = function (panel, movie, duration, isShowClose) {
|
||
if (!panel) return;
|
||
if (!movie) return;
|
||
panel.RemoveAndDeleteChildren();
|
||
$.CreatePanelWithProperties( "MoviePanel", panel, "MovieContentPanel", {class:"Movie", src: "file://{resources}/videos/" + movie + ".webm", repeat: "true", autoplay: "onload"} );
|
||
if (isShowClose === true) {
|
||
$.CreatePanelWithProperties( "Button", panel, "CloseMoviePlayButton", {onactivate: "$.GetContextPanel().ToggleClass('Hide')"} );
|
||
}
|
||
if (duration) {
|
||
$.Schedule(duration, function () {
|
||
panel.RemoveAndDeleteChildren();
|
||
});
|
||
}
|
||
}
|
||
|
||
|
||
//将系统API获取的颜色转化为html格式,如 i = Players.GetPlayerColor(id)
|
||
GameUI.DotaColorToHtml = function (i) {
|
||
return "#" + ('00' + ( i & 0xFF).toString( 16 ) ).substr( -2 ) +
|
||
('00' + ( ( i >> 8 ) & 0xFF ).toString( 16 ) ).substr( -2 ) +
|
||
('00' + ( ( i >> 16 ) & 0xFF ).toString( 16 ) ).substr( -2 ) +
|
||
('00' + ( ( i >> 24 ) & 0xFF ).toString( 16 ) ).substr( -2 );
|
||
};
|
||
|
||
|
||
//打印消息
|
||
GameUI.Print = function (msg) {
|
||
$.Msg("-------- " + Math.floor(Game.GetGameTime() * 100 + 0.5) / 100 + " : " + msg);
|
||
};
|
||
|
||
|
||
|
||
// Loading
|
||
function Loading() {
|
||
if (Game.GameStateIs(DOTA_GameState.DOTA_GAMERULES_STATE_CUSTOM_GAME_SETUP)) {
|
||
$("#loading_png").visible = false;
|
||
$("#loading_text").visible = false;
|
||
return;
|
||
}
|
||
$.Schedule(0.5, Loading);
|
||
}
|
||
|
||
;(function() {
|
||
GameUI.PlayMovie($("#MoviePlayPanel"), "promo/outlanders_bg");
|
||
Loading();
|
||
})(); |