Skip to content

Commit

Permalink
[FIX] 修复表达式结束判断; 修复无文本的脚本解析问题; 修复windows下导出文件夹无写入问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wetor committed Jun 5, 2024
1 parent ac23514 commit 2da8e6a
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
with:
artifacts: "MagesTools_linux,MagesTools_mac,MagesTools_win.exe"
bodyFile: ""
token: ${{ secrets.RELEASE_GITHUB_TOKEN }}
token: ${{ secrets.GITHUB_TOKEN }}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ MagesTools -type=diff \

## 更新日志

### 2024.6.5
- 修复表达式结束判断
- 复无文本的脚本解析问题
- 修复windows下导出文件夹无写入问题
- (以上问题由 [Fluchw](https://github.com/wetor/MagesTools/issues/5) 发现)

### 2022.10.21
- 修复':'字符后为字节数据(`:[0xFF]`)导致Encode错误的问题 ([kurikomoe](https://github.com/kurikomoe)发现)

Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package main

import (
"flag"
"fmt"
"path/filepath"
"strings"

"MagesTools/script"
"MagesTools/script/format"
"MagesTools/script/utils"
"flag"
"fmt"
"github.com/go-restruct/restruct"
"path"
"strings"
)

func main() {

fmt.Print(`MagesTools
Version: 0.2.2_2022.10.21
Version: 0.2.3_2024.06.05
Author: WéΤοr (wetorx@qq.com)
Github: https://github.com/wetor/MagesTools
License: GPL-3.0
Expand Down Expand Up @@ -102,7 +103,7 @@ License: GPL-3.0
scr.Open(file, _format)
scr.Read()
// 导出
scr.SaveStrings(path.Join(pOutput, path.Base(file)+".txt"))
scr.SaveStrings(filepath.Join(pOutput, filepath.Base(file)+".txt"))
}
} else if utils.IsFile(pSource) && utils.IsFile(pOutput) {
scr.Open(pSource, _format)
Expand Down
41 changes: 26 additions & 15 deletions script/format/NpcsFormat.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package format

import (
"MagesTools/script/utils"
"bytes"
"fmt"
"strings"

"MagesTools/script/utils"
)

type Npcs struct {
Expand Down Expand Up @@ -91,20 +92,30 @@ func (f *Npcs) DecodeLine(data []byte) string {
tmp := bytes.NewBuffer(nil)
tmp.WriteByte(data[i])
i++
for !(data[i] == 0 && data[i+1] == 0) {
switch data[i] & 0x60 {
case 0: //1 byte
tmp.WriteByte(data[i])
i++
case 0x20: //2 byte
tmp.Write(data[i : i+2])
i += 2
case 0x40: //3 byte
tmp.Write(data[i : i+3])
i += 3
case 0x60: // le int32 4 byte
tmp.Write(data[i : i+4])
i += 4
for {
//for !(data[i] == 0 && data[i+1] == 0) {
if (data[i] & 0x80) == 0x80 {
switch data[i] & 0x60 {
case 0: //1 byte
tmp.WriteByte(data[i])
i++
case 0x20: //2 byte
tmp.Write(data[i : i+2])
i += 2
case 0x40: //3 byte
tmp.Write(data[i : i+3])
i += 3
case 0x60: // le int32 4 byte
tmp.Write(data[i : i+4])
i += 4
}
} else {
if data[i] == 0 {
break
} else {
tmp.WriteByte(data[i])
i++
}
}
}
tmp.WriteByte(data[i])
Expand Down
41 changes: 26 additions & 15 deletions script/format/NpcsPFormat.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package format

import (
"MagesTools/script/utils"
"bytes"
"fmt"
"strings"

"MagesTools/script/utils"
)

type NpcsP struct {
Expand Down Expand Up @@ -86,20 +87,30 @@ func (f *NpcsP) DecodeLine(data []byte) string {
tmp := bytes.NewBuffer(nil)
tmp.WriteByte(data[i])
i++
for !(data[i] == 0 && data[i+1] == 0) {
switch data[i] & 0x60 {
case 0: //1 byte
tmp.WriteByte(data[i])
i++
case 0x20: //2 byte
tmp.Write(data[i : i+2])
i += 2
case 0x40: //3 byte
tmp.Write(data[i : i+3])
i += 3
case 0x60: // le int32 4 byte
tmp.Write(data[i : i+4])
i += 4
for {
//for !(data[i] == 0 && data[i+1] == 0) {
if (data[i] & 0x80) == 0x80 {
switch data[i] & 0x60 {
case 0: //1 byte
tmp.WriteByte(data[i])
i++
case 0x20: //2 byte
tmp.Write(data[i : i+2])
i += 2
case 0x40: //3 byte
tmp.Write(data[i : i+3])
i += 3
case 0x60: // le int32 4 byte
tmp.Write(data[i : i+4])
i += 4
}
} else {
if data[i] == 0 {
break
} else {
tmp.WriteByte(data[i])
i++
}
}
}
tmp.WriteByte(data[i])
Expand Down
6 changes: 5 additions & 1 deletion script/sc3.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package script

import (
"MagesTools/script/utils"
"bytes"
"encoding/binary"
"fmt"

"MagesTools/script/utils"
"github.com/go-restruct/restruct"
)

Expand Down Expand Up @@ -33,6 +34,9 @@ func (s *Sc3) ReadStrings(readString func([]byte) string) {
panic(err)
}
s.Count = (s.OffsetEnd - s.OffsetStart) / 4
if s.Count == 0 {
return
}
s.Offsets = make([]Entry, s.Count)
offset := s.OffsetStart
for i := 0; i < s.Count; i++ {
Expand Down
62 changes: 35 additions & 27 deletions script/script.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package script

import (
"MagesTools/script/format"
"MagesTools/script/utils"
"bufio"
"fmt"
"io"
"os"
"path/filepath"

"MagesTools/script/format"
"MagesTools/script/utils"
)

type Entry struct {
Expand Down Expand Up @@ -38,35 +41,37 @@ type Strings interface {
}

type Script struct {
Name string
Strings Strings
Format format.Format
DecodeCharset map[uint16]string
EncodeCharset map[string]uint16
}

// NewScript
// Description 打开脚本文件
// Param filename string
// Return *Script
//
// Description 打开脚本文件
// Param filename string
// Return *Script
func NewScript(filename string, format format.Format) *Script {
script := &Script{}
script.Open(filename, format)
return script
}

// Open
// Description 打开脚本文件,如果已经使用LoadCharset载入码表,则不需要重新调用LoadCharset
// Receiver s *Script
// Param filename string
// Param format format.Format
//
// Description 打开脚本文件,如果已经使用LoadCharset载入码表,则不需要重新调用LoadCharset
// Receiver s *Script
// Param filename string
// Param format format.Format
func (s *Script) Open(filename string, format format.Format) {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer f.Close()
s.Name = filepath.Base(filename)
data, err := io.ReadAll(f)
if err != nil {
panic(err)
Expand All @@ -83,12 +88,12 @@ func (s *Script) Open(filename string, format format.Format) {
}

// LoadCharset
// Description 载入码表/字符集
// Receiver s *Script
// Param filename string 文件名
// Param isTBL bool 是否为码表。否则为字符集,字符集从0x8000开始
// Param skipExist bool 是否检查并跳过重复出现的字符,仅以第一次出现为准
//
// Description 载入码表/字符集
// Receiver s *Script
// Param filename string 文件名
// Param isTBL bool 是否为码表。否则为字符集,字符集从0x8000开始
// Param skipExist bool 是否检查并跳过重复出现的字符,仅以第一次出现为准
func (s *Script) LoadCharset(filename string, isTBL, skipExist bool) {

f, err := os.Open(filename)
Expand Down Expand Up @@ -139,9 +144,9 @@ func (s *Script) LoadCharset(filename string, isTBL, skipExist bool) {
}

// Read
// Description 解析文本,需要至少执行一次script.LoadCharset载入码表
// Receiver s *Script
//
// Description 解析文本,需要至少执行一次script.LoadCharset载入码表
// Receiver s *Script
func (s *Script) Read() {
if s.DecodeCharset != nil && s.EncodeCharset != nil {
s.Format.SetCharset(s.DecodeCharset, s.EncodeCharset)
Expand All @@ -150,25 +155,28 @@ func (s *Script) Read() {
}

// SaveStrings
// Description 保存文本,需要先执行script.Read
// Receiver s *Script
// Param filename string
//
// Description 保存文本,需要先执行script.Read
// Receiver s *Script
// Param filename string
func (s *Script) SaveStrings(filename string) {
strings := s.Strings.GetStrings()
if len(strings) == 0 {
fmt.Printf("文件 %s 无任何文本,跳过\n", s.Name)
return
}
f, _ := os.Create(filename)
defer f.Close()

strings := s.Strings.GetStrings()
for _, str := range strings {
f.WriteString(str + "\n")
}
}

// LoadStrings
// Description 载入文本并导入
// Receiver s *Script
// Param filename string
//
// Description 载入文本并导入
// Receiver s *Script
// Param filename string
func (s *Script) LoadStrings(filename string) {
f, _ := os.Open(filename)
defer f.Close()
Expand All @@ -183,10 +191,10 @@ func (s *Script) LoadStrings(filename string) {
}

// Write
// Description 保存为脚本
// Receiver s *Script
// Param filename string
//
// Description 保存为脚本
// Receiver s *Script
// Param filename string
func (s *Script) Write(filename string) {
s.Strings.WriteStrings(s.Format.EncodeLine)

Expand Down

0 comments on commit 2da8e6a

Please sign in to comment.