forked from skydark/nstools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7053116
Showing
23 changed files
with
47,953 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from getopt import gnu_getopt, GetoptError | ||
from ast import literal_eval | ||
import sys | ||
import re | ||
|
||
|
||
class Option(object): | ||
|
||
def __init__(self, short=None, long=None, value=False, parse=None): | ||
self.is_flag = True | ||
if parse: | ||
split = parse.strip().split(' ') | ||
options = split[0].replace(',', ' ').replace('=', ' ') | ||
description = ''.join(split[1:]) | ||
for s in options.split(): | ||
if s.startswith('--'): | ||
long = s.lstrip('-') | ||
elif s.startswith('-'): | ||
short = s.lstrip('-') | ||
else: | ||
self.is_flag = False | ||
if not self.is_flag: | ||
matched = re.findall('\[default: (.*)\]', description) | ||
value = argument_eval(matched[0]) if matched else False | ||
short = short + ':' if short else None | ||
long = long + '=' if long else None | ||
self.short = short | ||
self.long = long | ||
self.value = value | ||
|
||
@property | ||
def name(self): | ||
s = self.long or self.short | ||
s = s.rstrip(':').rstrip('=') | ||
ret = s[0] if s[0].isalpha() else '_' | ||
for ch in s[1:]: | ||
ret += ch if ch.isalpha() or ch.isdigit() else '_' | ||
return ret | ||
|
||
@property | ||
def forms(self): | ||
if self.short: | ||
yield '-' + self.short.rstrip(':') | ||
if self.long: | ||
yield '--' + self.long.rstrip('=') | ||
|
||
def __repr__(self): | ||
return 'Option(%s, %s, %s)' % (repr(self.short), | ||
repr(self.long), | ||
repr(self.value)) | ||
|
||
def __eq__(self, other): | ||
return repr(self) == repr(other) | ||
|
||
def __ne__(self, other): | ||
return not self == other | ||
|
||
|
||
class Options(object): | ||
|
||
def __init__(self, **kw): | ||
self.__dict__ = kw | ||
|
||
def __eq__(self, other): | ||
return type(self) is type(other) and \ | ||
self.__dict__ == other.__dict__ | ||
|
||
def __ne__(self, other): | ||
return not self == other | ||
|
||
def __repr__(self): | ||
return 'Options(%s)' % ',\n '.join("%s=%s" % (kw, repr(a)) | ||
for kw, a in self.__dict__.items()) | ||
|
||
|
||
def argument_eval(s): | ||
try: | ||
return literal_eval(s) | ||
except (ValueError, SyntaxError): | ||
return s | ||
|
||
|
||
def docopt(doc, args=sys.argv[1:], help=True, version=None): | ||
docopts = [Option(parse='-' + s) for s in re.split('^ *-|\n *-', doc)[1:]] | ||
try: | ||
getopts, args = gnu_getopt(args, | ||
''.join(d.short for d in docopts if d.short), | ||
[d.long for d in docopts if d.long]) | ||
except GetoptError as e: | ||
exit(e.msg) | ||
for k, v in getopts: | ||
for o in docopts: | ||
if k in o.forms: | ||
o.value = True if o.is_flag else argument_eval(v) | ||
if help and k in ('-h', '--help'): | ||
exit(doc.strip()) | ||
if version is not None and k == '--version': | ||
exit(version) | ||
return Options(**dict((o.name, o.value) for o in docopts)), args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
;用来存放flag的结构 | ||
|
||
; 初始化结构 | ||
; numalias Address, 100 | ||
; numalias Length, 5 | ||
; >>> flags_init Address, Length | ||
; $Address = "00000" & %Address = 5 | ||
@def flags_init %1, %2 | ||
mov %%1, %2 | ||
mov $%1, "" | ||
for %2 = %2 to 1 step -1 | ||
add $%1, "0" | ||
next | ||
return | ||
|
||
; 设置某个位置的flag | ||
; 注意,flag的位置从1到Length, 并只能将该位置的值设置为0~9中的数值 | ||
; >>> flags_set Address, 3, 2 | ||
; $Address = "00200" | ||
@def flags_set %1, %2, %3 | ||
if %3 > 9 mov %3, 9:skip 2 | ||
if %3 < 0 mov %3, 0 | ||
mid $1, $%1, %2, %%1-%2 | ||
mid $%1, $%1, 0, %2-1 | ||
itoa $2, %3 | ||
add $%1, $2 | ||
add $%1, $1 | ||
return | ||
|
||
; 取得某个位置的flag | ||
; numalias Value, 1 | ||
; >>> flags_get %Value, Address, 3 | ||
; %Value = 2 | ||
@def flags_get i%3, %1, %2 | ||
mid $1, $%1, %2-1, 1 | ||
atoi %%3, $1 | ||
return | ||
|
||
; 将某个位置的flag的值增加一定的值 | ||
; 最后一个参数是增加的值,用负数即可表示减少 | ||
; 注意,最终值仍然被限定在0~9之间 | ||
; >>> flags_change Address, 3, 4 | ||
; $Address = "00600" | ||
@def flags_change %1, %2, %3 | ||
mid $1, $%1, %2, %%1-%2 | ||
mid $2, $%1, %2-1, 1 | ||
mid $%1, $%1, 0, %2-1 | ||
atoi %2, $2 | ||
add %3, %2 | ||
if %3 > 9 mov %3, 9:skip 2 | ||
if %3 < 0 mov %3, 0 | ||
itoa $2, %3 | ||
add $%1, $2 | ||
add $%1, $1 | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
吧 巴 | ||
么 莫 | ||
飞 飛 | ||
她 他 | ||
发 發 | ||
啊 阿 | ||
跑 袍 | ||
吗 馬 | ||
还 還 | ||
哪 那 | ||
呢 尼 | ||
呐 肭 | ||
嗯 恩 | ||
你 尼 | ||
啦 拉 | ||
哎 艾 | ||
瞪 登 | ||
吓 下 | ||
够 勾 | ||
內 内 | ||
佈 布 | ||
腳 脚 | ||
值 値 | ||
增 増 | ||
踢 裼 | ||
咱 俺 | ||
哼 亨 | ||
呃 厄 | ||
— 一 | ||
- 一 | ||
~ ‾ | ||
" 」 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" Convert GBK nscripter dat to SHIFT-JIS version | ||
Usage: gbk2sjis.py [options] origin_file output_file=['./out.txt'] | ||
or gbk2sjis.py [options] origin_directory output_file=['./out.txt'] | ||
Options: | ||
-h --help show this help | ||
-m --method METHOD set method for missing characters, auto/manual [default: auto] | ||
""" | ||
|
||
from os import path | ||
import sys | ||
import codecs | ||
import random | ||
|
||
from docopt import docopt | ||
|
||
from ons.handler import ONSHandler | ||
from portable import decode, encode, chr, UEMPTY, py3k | ||
|
||
|
||
def help(): | ||
exit(__doc__.strip()) | ||
|
||
|
||
def hzconvert(text, from_, to_, method='auto', chardict=None): | ||
assert from_ == 'gbk' and to_ == 'sjis' and method == 'auto' | ||
|
||
from zhtools import chconv, xpinyin | ||
cdict = chconv.Chinese2Kanji_Table | ||
for k, v in chardict.items(): | ||
try: | ||
encode(v, 'SHIFT-JIS') | ||
cdict[ord(k)] = ord(v) | ||
except UnicodeEncodeError: | ||
pass | ||
|
||
xpy = xpinyin.Pinyin() | ||
guess_chars = set() | ||
except_chars = set() | ||
|
||
def gbk_to_sjis(exc): | ||
if not isinstance(exc, UnicodeEncodeError): | ||
raise exc | ||
newpos = exc.end | ||
char = exc.object[exc.start:exc.end] | ||
c = ord(char) | ||
if c in cdict: | ||
# print('%s: %s matched!' %(char, cdict[c])) | ||
return chr(cdict[c]), newpos | ||
pinyin = xpy.get_pinyin(char) | ||
ok = [] | ||
if pinyin: | ||
for newchar in xpy.py2hz(pinyin): | ||
try: | ||
encode(newchar, 'SHIFT-JIS') | ||
ok.append(newchar) | ||
except UnicodeEncodeError: | ||
pass | ||
for newchar in xpy.py2hz(pinyin[:-1]): | ||
try: | ||
encode(newchar, 'SHIFT-JIS') | ||
ok.append(newchar) | ||
except UnicodeEncodeError: | ||
pass | ||
if ok: | ||
newchar = random.choice(ok) | ||
cdict[c] = ord(newchar) | ||
guess_chars.add(c) | ||
# print('%s: %s' %(char, ','.join(ok))) | ||
return newchar, newpos | ||
except_chars.add(c) | ||
# print('Can not encode %s, ignore' % char) | ||
return ' ' * (newpos - exc.start), newpos | ||
|
||
codecs.register_error('gbk_to_sjis', gbk_to_sjis) | ||
# from zhtools import langconv | ||
# text = langconv.Converter('zh-hant').convert(text) | ||
try: | ||
text = text.encode('SHIFT-JIS', errors='gbk_to_sjis') | ||
except UnicodeError as exc: | ||
char = exc.object[exc.start:exc.end] | ||
print(char) | ||
raise | ||
print('These chars cannot encode to shift-jis:') | ||
if py3k: | ||
print(''.join(chr(c) for c in except_chars)) | ||
else: | ||
print(encode(UEMPTY.join(chr(c) for c in except_chars))) | ||
print('These chars can be guessed by pinyin:') | ||
if py3k: | ||
print(''.join(chr(c) for c in guess_chars)) | ||
else: | ||
print(encode(UEMPTY.join(chr(c) for c in guess_chars))) | ||
return text | ||
|
||
|
||
def read_char_dict(data_path, encoding=None): | ||
chardict = {} | ||
try: | ||
buf = open(data_path, 'rb').read() | ||
buf = decode(buf, encoding) | ||
for data in buf.splitlines(): | ||
k, v = data.strip().split(' ', 1) | ||
chardict[k] = v | ||
except Exception as e: | ||
print(e) | ||
else: | ||
print('Success load char dict: %s, %s' % (data_path, len(chardict))) | ||
return chardict | ||
|
||
|
||
def main(args, gui=True): | ||
if len(args) <= 1 and gui: | ||
# Show GUI | ||
from portable import tkFileDialog | ||
|
||
def file_or_directory(p): | ||
filename = path.basename(p) | ||
dirname = path.dirname(p) | ||
if filename == 'nscript.dat': | ||
return dirname | ||
base, ext = path.splitext(filename) | ||
if ext != '.txt': | ||
return dirname | ||
if base.isdigit() and 0 <= int(base) < 100: | ||
return dirname | ||
return p | ||
|
||
in_path = tkFileDialog.askopenfilename(title='请选择脚本文件') | ||
in_path = file_or_directory(in_path) | ||
print('in_path: ' + in_path) | ||
if not in_path: | ||
exit() | ||
out_path = tkFileDialog.asksaveasfilename(title='请选择要保存的文件名') | ||
if not out_path: | ||
exit() | ||
method = 'auto' | ||
else: | ||
options, arguments = docopt(__doc__) | ||
if len(arguments) == 1: | ||
in_path = arguments[0] | ||
out_path = './out.txt' | ||
elif len(arguments) != 2: | ||
help() | ||
else: | ||
in_path = arguments[0] | ||
out_path = arguments[1] | ||
method = options.method | ||
if method != 'auto': | ||
exit('Not Implemented Method: %s' % method) | ||
|
||
try: | ||
data_path = path.join(path.dirname(path.abspath(__file__)), 'gbk2sjis.dat') | ||
chardict = read_char_dict(data_path) | ||
|
||
ons = ONSHandler(in_path) | ||
text = ons.get_script() | ||
text = hzconvert(text, 'gbk', 'sjis', method, chardict) | ||
|
||
open(out_path, 'wb').write(text) | ||
except Exception as e: | ||
if gui: | ||
from portable import tkMessageBox | ||
tkMessageBox.showerror(message=repr(e), title='Error!') | ||
exit() | ||
raise | ||
else: | ||
if gui: | ||
from portable import tkMessageBox | ||
tkMessageBox.showinfo(message='转换完成!', title='Finished!') | ||
|
||
|
||
if __name__ == '__main__': | ||
main(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
; 我是被导入的段落 | ||
@python | ||
@output = __ | ||
@__ = lambda s: output('; 证明我修改了输出: '+s) | ||
@__('我的名字是: {__current_name}') | ||
@__base += 500 | ||
@__('我把基址增加了500, 我的基址是: {__base}') | ||
@## 单引号里 @{var} 和 {var} 都可以使用 | ||
@__('之前的内嵌 python 脚本使用 for 循环,变量 name 的值是 @{name}') | ||
@name = '新名字' | ||
@__('我把它改成了 {name}') | ||
|
||
@def test2 %1 | ||
测试输出BASE @{__base} | ||
click | ||
return | ||
|
Oops, something went wrong.