[add] Set CocosBridge(Cocos to Web)

This commit is contained in:
建喵 2022-08-29 15:17:11 +08:00
parent ecad6d01e3
commit 1606c28b5c
7 changed files with 645 additions and 626 deletions

View File

@ -1,125 +1,125 @@
/** /**
* 回呼函數: fnname (arg: TArg): void * 回呼函數: fnname (arg: TArg): void
*/ */
interface ActionCallback<TArg> { interface ActionCallback<TArg> {
(arg: TArg): void; (arg: TArg): void;
} }
interface Struct<TArg> { interface Struct<TArg> {
callback: ActionCallback<TArg>; callback: ActionCallback<TArg>;
target: any; target: any;
once?: boolean; once?: boolean;
} }
export class Action<TArg> { export class Action<TArg> {
private _queue: Struct<TArg>[] = []; private _queue: Struct<TArg>[] = [];
/** /**
* *
* @param callback 回呼函數: fnname (arg: TArg): void * @param callback 回呼函數: fnname (arg: TArg): void
* @param bindTarget this綁定的對象 * @param bindTarget this綁定的對象
*/ */
AddCallback(callback: ActionCallback<TArg>, bindTarget?: any) { AddCallback(callback: ActionCallback<TArg>, bindTarget?: any) {
let q = <Struct<TArg>> { let q = <Struct<TArg>> {
callback: callback, callback: callback,
target: bindTarget target: bindTarget
}; };
this._queue.push(q); this._queue.push(q);
} }
/** /**
* () * ()
* @param callback 回呼函數: fnname (arg: TArg): void * @param callback 回呼函數: fnname (arg: TArg): void
* @param bindTarget this綁定的對象 * @param bindTarget this綁定的對象
*/ */
AddCallbackOnce(callback: ActionCallback<TArg>, bindTarget?: any) { AddCallbackOnce(callback: ActionCallback<TArg>, bindTarget?: any) {
let q = <Struct<TArg>> { let q = <Struct<TArg>> {
callback: callback, callback: callback,
target: bindTarget, target: bindTarget,
once: true once: true
}; };
this._queue.push(q); this._queue.push(q);
} }
/** /**
* *
* @param callback * @param callback
*/ */
RemoveByCallback(callback: ActionCallback<TArg>) { RemoveByCallback(callback: ActionCallback<TArg>) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback || q.callback === callback) { if (!q.callback || q.callback === callback) {
q.callback = undefined; q.callback = undefined;
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
/** /**
* *
* @param bindTarget this綁定的對象 * @param bindTarget this綁定的對象
*/ */
RemoveByBindTarget(bindTarget: any) { RemoveByBindTarget(bindTarget: any) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback || q.target === bindTarget) { if (!q.callback || q.target === bindTarget) {
q.callback = undefined; q.callback = undefined;
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
/** /**
* *
*/ */
RemoveAllCallbacks() { RemoveAllCallbacks() {
this._queue.forEach(q => q.callback = undefined); this._queue.forEach(q => q.callback = undefined);
this._queue.length = 0; this._queue.length = 0;
} }
/** /**
* *
* @param arg * @param arg
*/ */
DispatchCallback(arg: TArg) { DispatchCallback(arg: TArg) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
let cleanRemoved = false; let cleanRemoved = false;
this._queue.slice().forEach(q => { this._queue.slice().forEach(q => {
if (!q.callback) { if (!q.callback) {
cleanRemoved = true; cleanRemoved = true;
return; return;
} }
if (q.target) { if (q.target) {
q.callback.call(q.target, arg); q.callback.call(q.target, arg);
} else { } else {
q.callback(arg); q.callback(arg);
} }
if (q.once) { if (q.once) {
q.callback = undefined; q.callback = undefined;
cleanRemoved = true; cleanRemoved = true;
} }
}); });
if (cleanRemoved) { if (cleanRemoved) {
index = this._queue.length; index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback) { if (!q.callback) {
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
} }
} }
} }

View File

@ -1,85 +1,85 @@
import { Action } from "./Action"; import { Action } from "./Action";
import { ActionWithType } from "./ActionWithType"; import { ActionWithType } from "./ActionWithType";
import { ActionWithType2 } from "./ActionWithType2"; import { ActionWithType2 } from "./ActionWithType2";
const {ccclass, property} = cc._decorator; const {ccclass, property} = cc._decorator;
enum CustomType { enum CustomType {
Ex1, Ex2 Ex1, Ex2
} }
class CustomEvent extends ActionWithType<CustomType, number> {} class CustomEvent extends ActionWithType<CustomType, number> {}
class CustomEvent2 extends ActionWithType2<CustomType, number> {} class CustomEvent2 extends ActionWithType2<CustomType, number> {}
@ccclass @ccclass
export default class NewClass extends cc.Component { export default class NewClass extends cc.Component {
callback: Action<number> = new Action<number>(); callback: Action<number> = new Action<number>();
customCallback: CustomEvent = new CustomEvent(); customCallback: CustomEvent = new CustomEvent();
customCallback2: CustomEvent2 = new CustomEvent2(); customCallback2: CustomEvent2 = new CustomEvent2();
private num: number = 0; private num: number = 0;
start () { start () {
this.callback.AddCallback(this.CB, this); this.callback.AddCallback(this.CB, this);
this.callback.AddCallbackOnce(this.OnceCB, this); this.callback.AddCallbackOnce(this.OnceCB, this);
this.customCallback.AddCallback(CustomType.Ex1, this.CBType, this); this.customCallback.AddCallback(CustomType.Ex1, this.CBType, this);
this.customCallback.AddCallbackOnce(CustomType.Ex2, this.OnceCBType, this); this.customCallback.AddCallbackOnce(CustomType.Ex2, this.OnceCBType, this);
this.customCallback2.AddCallback(CustomType.Ex2, this.CBTypeAllin1, this); this.customCallback2.AddCallback(CustomType.Ex2, this.CBTypeAllin1, this);
this.customCallback2.AddCallbackOnce(CustomType.Ex1, this.CBTypeAllin1, this); this.customCallback2.AddCallbackOnce(CustomType.Ex1, this.CBTypeAllin1, this);
} }
DispatchClick() { DispatchClick() {
this.num++; this.num++;
this.callback.DispatchCallback(this.num); this.callback.DispatchCallback(this.num);
this.customCallback.DispatchCallback(CustomType.Ex1, this.num); this.customCallback.DispatchCallback(CustomType.Ex1, this.num);
this.customCallback.DispatchCallback(CustomType.Ex2, this.num); this.customCallback.DispatchCallback(CustomType.Ex2, this.num);
this.customCallback2.DispatchCallback(CustomType.Ex1, this.num); this.customCallback2.DispatchCallback(CustomType.Ex1, this.num);
this.customCallback2.DispatchCallback(CustomType.Ex2, this.num); this.customCallback2.DispatchCallback(CustomType.Ex2, this.num);
} }
RemoveEventClick() { RemoveEventClick() {
this.callback.RemoveByCallback(this.CB); this.callback.RemoveByCallback(this.CB);
// this.callback.RemoveByCallback(this.OnceCB); // this.callback.RemoveByCallback(this.OnceCB);
// this.callback.RemoveByBindTarget(this); // this.callback.RemoveByBindTarget(this);
// this.callback.RemoveAll(); // this.callback.RemoveAll();
// this.callbackWithType.RemoveByCallback(this.CBType); // this.callbackWithType.RemoveByCallback(this.CBType);
// this.callbackWithType.RemoveByCallback(this.OnceCBType); // this.callbackWithType.RemoveByCallback(this.OnceCBType);
this.customCallback.RemoveByType(CustomType.Ex1); this.customCallback.RemoveByType(CustomType.Ex1);
// this.callbackWithType.RemoveByType(CustomType.Ex2); // this.callbackWithType.RemoveByType(CustomType.Ex2);
// this.callbackWithType.RemoveByBindTarget(this); // this.callbackWithType.RemoveByBindTarget(this);
// this.callbackWithType.RemoveAll(); // this.callbackWithType.RemoveAll();
} }
OnceCB(x: number) { OnceCB(x: number) {
cc.log(`OnceCB [${this.num}]`); cc.log(`OnceCB [${this.num}]`);
} }
CB(x: number) { CB(x: number) {
cc.log(`CB [${this.num}]`); cc.log(`CB [${this.num}]`);
} }
OnceCBType(x: number) { OnceCBType(x: number) {
cc.log(`OnceCBType [${this.num}]`); cc.log(`OnceCBType [${this.num}]`);
} }
CBType(x: number) { CBType(x: number) {
cc.log(`CBType [${this.num}]`); cc.log(`CBType [${this.num}]`);
} }
CBTypeAllin1(type: CustomType,x: number) { CBTypeAllin1(type: CustomType,x: number) {
// switch (type) { // switch (type) {
// case CustomType.Ex1: // case CustomType.Ex1:
// break; // break;
// case CustomType.Ex2: // case CustomType.Ex2:
// break; // break;
// } // }
cc.log(`CBTypeAllin1 [${CustomType[type]}][${this.num}]`); cc.log(`CBTypeAllin1 [${CustomType[type]}][${this.num}]`);
} }
} }

View File

@ -1,166 +1,166 @@
/** /**
* 回呼函數: fnname (arg: TArg): void * 回呼函數: fnname (arg: TArg): void
*/ */
interface ActionCallback<TArg> { interface ActionCallback<TArg> {
(arg: TArg): void; (arg: TArg): void;
} }
interface Struct<TType, TArg> { interface Struct<TType, TArg> {
callback: ActionCallback<TArg>; callback: ActionCallback<TArg>;
target: any; target: any;
type: TType; type: TType;
once?: boolean; once?: boolean;
} }
export class ActionWithType<TType, TArg> { export class ActionWithType<TType, TArg> {
private _queue: Struct<TType, TArg>[] = []; private _queue: Struct<TType, TArg>[] = [];
/** /**
* *
* @param callback 回呼函數: fnname (arg: TArg): void * @param callback 回呼函數: fnname (arg: TArg): void
* @param bindTarget this綁定的對象 * @param bindTarget this綁定的對象
*/ */
AddCallback(type: TType, callback: ActionCallback<TArg>, bindTarget?: any) { AddCallback(type: TType, callback: ActionCallback<TArg>, bindTarget?: any) {
let q = <Struct<TType, TArg>> { let q = <Struct<TType, TArg>> {
callback: callback, callback: callback,
target: bindTarget, target: bindTarget,
type: type type: type
}; };
this._queue.push(q); this._queue.push(q);
} }
/** /**
* () * ()
* @param callback 回呼函數: fnname (arg: TArg): void * @param callback 回呼函數: fnname (arg: TArg): void
* @param bindTarget this綁定的對象 * @param bindTarget this綁定的對象
*/ */
AddCallbackOnce(type: TType, callback: ActionCallback<TArg>, bindTarget?: any) { AddCallbackOnce(type: TType, callback: ActionCallback<TArg>, bindTarget?: any) {
let q = <Struct<TType, TArg>> { let q = <Struct<TType, TArg>> {
callback: callback, callback: callback,
target: bindTarget, target: bindTarget,
type: type, type: type,
once: true once: true
}; };
this._queue.push(q); this._queue.push(q);
} }
/** /**
* *
* @param callback * @param callback
*/ */
RemoveByCallback(callback: ActionCallback<TArg>) { RemoveByCallback(callback: ActionCallback<TArg>) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback || q.callback === callback) { if (!q.callback || q.callback === callback) {
q.callback = undefined; q.callback = undefined;
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
/** /**
* *
* @param bindTarget this綁定的對象 * @param bindTarget this綁定的對象
*/ */
RemoveByBindTarget(bindTarget: any) { RemoveByBindTarget(bindTarget: any) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback || q.target === bindTarget) { if (!q.callback || q.target === bindTarget) {
q.callback = undefined; q.callback = undefined;
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
/** /**
* *
* @param type * @param type
*/ */
RemoveByType(type: TType) { RemoveByType(type: TType) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback || q.type === type) { if (!q.callback || q.type === type) {
q.callback = undefined; q.callback = undefined;
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
/** /**
* *
* @param type * @param type
* @param callback * @param callback
*/ */
RemoveCallback(type:TType, callback: ActionCallback<TArg>) { RemoveCallback(type:TType, callback: ActionCallback<TArg>) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback || (q.type === type && q.callback === callback)) { if (!q.callback || (q.type === type && q.callback === callback)) {
q.callback = undefined; q.callback = undefined;
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
/** /**
* *
*/ */
RemoveAllCallbacks() { RemoveAllCallbacks() {
this._queue.forEach(q => q.callback = undefined); this._queue.forEach(q => q.callback = undefined);
this._queue.length = 0; this._queue.length = 0;
} }
/** /**
* *
* @param type * @param type
* @param arg * @param arg
*/ */
DispatchCallback(type: TType, arg: TArg) { DispatchCallback(type: TType, arg: TArg) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
let cleanRemoved = false; let cleanRemoved = false;
this._queue.slice().forEach(q => { this._queue.slice().forEach(q => {
if (!q.callback) if (!q.callback)
{ {
cleanRemoved = true; cleanRemoved = true;
return; return;
} }
if (q.type !== type) return; if (q.type !== type) return;
if (q.target) { if (q.target) {
q.callback.call(q.target, arg); q.callback.call(q.target, arg);
} else { } else {
q.callback(arg); q.callback(arg);
} }
if (q.once) { if (q.once) {
q.callback = undefined; q.callback = undefined;
cleanRemoved = true; cleanRemoved = true;
} }
}); });
if (cleanRemoved) { if (cleanRemoved) {
index = this._queue.length; index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback) { if (!q.callback) {
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
} }
} }
} }

View File

@ -1,166 +1,166 @@
/** /**
* 回呼函數: fnname (type: TType, arg: TArg): void * 回呼函數: fnname (type: TType, arg: TArg): void
*/ */
interface ActionCallback<TType, TArg> { interface ActionCallback<TType, TArg> {
(type: TType, arg: TArg): void; (type: TType, arg: TArg): void;
} }
interface Struct<TType, TArg> { interface Struct<TType, TArg> {
callback: ActionCallback<TType, TArg>; callback: ActionCallback<TType, TArg>;
target: any; target: any;
type: TType; type: TType;
once?: boolean; once?: boolean;
} }
export class ActionWithType2<TType, TArg> { export class ActionWithType2<TType, TArg> {
private _queue: Struct<TType, TArg>[] = []; private _queue: Struct<TType, TArg>[] = [];
/** /**
* *
* @param callback 回呼函數: fnname (type: TType, arg: TArg): void * @param callback 回呼函數: fnname (type: TType, arg: TArg): void
* @param bindTarget this綁定的對象 * @param bindTarget this綁定的對象
*/ */
AddCallback(type: TType, callback: ActionCallback<TType, TArg>, bindTarget?: any) { AddCallback(type: TType, callback: ActionCallback<TType, TArg>, bindTarget?: any) {
let q = <Struct<TType, TArg>> { let q = <Struct<TType, TArg>> {
callback: callback, callback: callback,
target: bindTarget, target: bindTarget,
type: type type: type
}; };
this._queue.push(q); this._queue.push(q);
} }
/** /**
* () * ()
* @param callback 回呼函數: fnname (type: TType, arg: TArg): void * @param callback 回呼函數: fnname (type: TType, arg: TArg): void
* @param bindTarget this綁定的對象 * @param bindTarget this綁定的對象
*/ */
AddCallbackOnce(type: TType, callback: ActionCallback<TType, TArg>, bindTarget?: any) { AddCallbackOnce(type: TType, callback: ActionCallback<TType, TArg>, bindTarget?: any) {
let q = <Struct<TType, TArg>> { let q = <Struct<TType, TArg>> {
callback: callback, callback: callback,
target: bindTarget, target: bindTarget,
type: type, type: type,
once: true once: true
}; };
this._queue.push(q); this._queue.push(q);
} }
/** /**
* *
* @param callback * @param callback
*/ */
RemoveByCallback(callback: ActionCallback<TType, TArg>) { RemoveByCallback(callback: ActionCallback<TType, TArg>) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback || q.callback === callback) { if (!q.callback || q.callback === callback) {
q.callback = undefined; q.callback = undefined;
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
/** /**
* *
* @param bindTarget this綁定的對象 * @param bindTarget this綁定的對象
*/ */
RemoveByBindTarget(bindTarget: any) { RemoveByBindTarget(bindTarget: any) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback || q.target === bindTarget) { if (!q.callback || q.target === bindTarget) {
q.callback = undefined; q.callback = undefined;
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
/** /**
* *
* @param type * @param type
*/ */
RemoveByType(type: TType) { RemoveByType(type: TType) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback || q.type === type) { if (!q.callback || q.type === type) {
q.callback = undefined; q.callback = undefined;
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
/** /**
* *
* @param type * @param type
* @param callback * @param callback
*/ */
RemoveCallback(type:TType, callback: ActionCallback<TType, TArg>) { RemoveCallback(type:TType, callback: ActionCallback<TType, TArg>) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback || (q.type === type && q.callback === callback)) { if (!q.callback || (q.type === type && q.callback === callback)) {
q.callback = undefined; q.callback = undefined;
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
/** /**
* *
*/ */
RemoveAllCallbacks() { RemoveAllCallbacks() {
this._queue.forEach(q => q.callback = undefined); this._queue.forEach(q => q.callback = undefined);
this._queue.length = 0; this._queue.length = 0;
} }
/** /**
* *
* @param type * @param type
* @param arg * @param arg
*/ */
DispatchCallback(type: TType, arg: TArg) { DispatchCallback(type: TType, arg: TArg) {
let index = this._queue.length; let index = this._queue.length;
if (index > 0) { if (index > 0) {
let cleanRemoved = false; let cleanRemoved = false;
this._queue.slice().forEach(q => { this._queue.slice().forEach(q => {
if (!q.callback) if (!q.callback)
{ {
cleanRemoved = true; cleanRemoved = true;
return; return;
} }
if (q.type !== type) return; if (q.type !== type) return;
if (q.target) { if (q.target) {
q.callback.call(q.target, type, arg); q.callback.call(q.target, type, arg);
} else { } else {
q.callback(type, arg); q.callback(type, arg);
} }
if (q.once) { if (q.once) {
q.callback = undefined; q.callback = undefined;
cleanRemoved = true; cleanRemoved = true;
} }
}); });
if (cleanRemoved) { if (cleanRemoved) {
index = this._queue.length; index = this._queue.length;
if (index > 0) { if (index > 0) {
while (index--) { while (index--) {
let q = this._queue[index]; let q = this._queue[index];
if (!q.callback) { if (!q.callback) {
this._queue.splice(index, 1); this._queue.splice(index, 1);
} }
} }
} }
} }
} }
} }
} }

View File

@ -0,0 +1,9 @@
/** System類型 */
export enum System_Eevent {
/** SetFCMToken */
SetFCMToken,
/** Test */
Test
}

View File

@ -0,0 +1,10 @@
{
"ver": "1.1.0",
"uuid": "f0d14145-1175-48fa-b591-4b98f59a0e04",
"importer": "typescript",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@ -1,86 +1,86 @@
export module Encoding.UTF8 { export module Encoding.UTF8 {
export function GetBytes(str: string) { export function GetBytes(str: string) {
let len = str.length, resPos = -1; let len = str.length, resPos = -1;
let resArr = new Uint8Array(len * 3); let resArr = new Uint8Array(len * 3);
for (let point = 0, nextcode = 0, i = 0; i !== len;) { for (let point = 0, nextcode = 0, i = 0; i !== len;) {
point = str.charCodeAt(i), i += 1; point = str.charCodeAt(i), i += 1;
if (point >= 0xD800 && point <= 0xDBFF) { if (point >= 0xD800 && point <= 0xDBFF) {
if (i === len) { if (i === len) {
resArr[resPos += 1] = 0xef; resArr[resPos += 1] = 0xef;
resArr[resPos += 1] = 0xbf; resArr[resPos += 1] = 0xbf;
resArr[resPos += 1] = 0xbd; resArr[resPos += 1] = 0xbd;
break; break;
} }
nextcode = str.charCodeAt(i); nextcode = str.charCodeAt(i);
if (nextcode >= 0xDC00 && nextcode <= 0xDFFF) { if (nextcode >= 0xDC00 && nextcode <= 0xDFFF) {
point = (point - 0xD800) * 0x400 + nextcode - 0xDC00 + 0x10000; point = (point - 0xD800) * 0x400 + nextcode - 0xDC00 + 0x10000;
i += 1; i += 1;
if (point > 0xffff) { if (point > 0xffff) {
resArr[resPos += 1] = (0x1e << 3) | (point >>> 18); resArr[resPos += 1] = (0x1e << 3) | (point >>> 18);
resArr[resPos += 1] = (0x2 << 6) | ((point >>> 12) & 0x3f); resArr[resPos += 1] = (0x2 << 6) | ((point >>> 12) & 0x3f);
resArr[resPos += 1] = (0x2 << 6) | ((point >>> 6) & 0x3f); resArr[resPos += 1] = (0x2 << 6) | ((point >>> 6) & 0x3f);
resArr[resPos += 1] = (0x2 << 6) | (point & 0x3f); resArr[resPos += 1] = (0x2 << 6) | (point & 0x3f);
continue; continue;
} }
} else { } else {
resArr[resPos += 1] = 0xef; resArr[resPos += 1] = 0xef;
resArr[resPos += 1] = 0xbf; resArr[resPos += 1] = 0xbf;
resArr[resPos += 1] = 0xbd; resArr[resPos += 1] = 0xbd;
continue; continue;
} }
} }
if (point <= 0x007f) { if (point <= 0x007f) {
resArr[resPos += 1] = (0x0 << 7) | point; resArr[resPos += 1] = (0x0 << 7) | point;
} else if (point <= 0x07ff) { } else if (point <= 0x07ff) {
resArr[resPos += 1] = (0x6 << 5) | (point >>> 6); resArr[resPos += 1] = (0x6 << 5) | (point >>> 6);
resArr[resPos += 1] = (0x2 << 6) | (point & 0x3f); resArr[resPos += 1] = (0x2 << 6) | (point & 0x3f);
} else { } else {
resArr[resPos += 1] = (0xe << 4) | (point >>> 12); resArr[resPos += 1] = (0xe << 4) | (point >>> 12);
resArr[resPos += 1] = (0x2 << 6) | ((point >>> 6) & 0x3f); resArr[resPos += 1] = (0x2 << 6) | ((point >>> 6) & 0x3f);
resArr[resPos += 1] = (0x2 << 6) | (point & 0x3f); resArr[resPos += 1] = (0x2 << 6) | (point & 0x3f);
} }
} }
return resArr.subarray(0, resPos + 1); return resArr.subarray(0, resPos + 1);
} }
export function GetString(array: Uint8Array) { export function GetString(array: Uint8Array) {
let str = ""; let str = "";
let i = 0, len = array.length; let i = 0, len = array.length;
while (i < len) { while (i < len) {
let c = array[i++]; let c = array[i++];
switch (c >> 4) { switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
str += String.fromCharCode(c); str += String.fromCharCode(c);
break; break;
case 12: case 13: case 12: case 13:
str += String.fromCharCode(((c & 0x1F) << 6) | (array[i++] & 0x3F)); str += String.fromCharCode(((c & 0x1F) << 6) | (array[i++] & 0x3F));
break; break;
case 14: case 14:
str += String.fromCharCode(((c & 0x0F) << 12) | ((array[i++] & 0x3F) << 6) | ((array[i++] & 0x3F) << 0)); str += String.fromCharCode(((c & 0x0F) << 12) | ((array[i++] & 0x3F) << 6) | ((array[i++] & 0x3F) << 0));
break; break;
} }
} }
return str; return str;
} }
export function b64EncodeUnicode(str) { export function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
return String.fromCharCode('0x' + p1); return String.fromCharCode('0x' + p1);
})); }));
} }
export function b64DecodeUnicode(str) { export function b64DecodeUnicode(str) {
return decodeURIComponent(atob(str).split('').map(function (c) { return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join('')); }).join(''));
} }
export function isBase64(str) { export function isBase64(str) {
if (str === '' || str.trim() === '') { return false; } if (str === '' || str.trim() === '') { return false; }
try { try {
return btoa(atob(str)) == str; return btoa(atob(str)) == str;
} catch (err) { } catch (err) {
return false; return false;
} }
} }
} }