refactor(common): 重构 Conn 实体并优化地图进入逻辑
- 优化 Conn 实体的 SendPack 方法,提高代码复用性 - 添加 goja 模块到 go.work 文件 - 重构地图进入逻辑,增加玩家广播和刷怪功能 - 调整 OutInfo 结构中的 Vip 和 Viped 字段类型 - 简化 MonsterRefresh 结构体定义
This commit is contained in:
110
common/utils/goja/file/README.markdown
Normal file
110
common/utils/goja/file/README.markdown
Normal file
@@ -0,0 +1,110 @@
|
||||
# file
|
||||
--
|
||||
import "github.com/dop251/goja/file"
|
||||
|
||||
Package file encapsulates the file abstractions used by the ast & parser.
|
||||
|
||||
## Usage
|
||||
|
||||
#### type File
|
||||
|
||||
```go
|
||||
type File struct {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
#### func NewFile
|
||||
|
||||
```go
|
||||
func NewFile(filename, src string, base int) *File
|
||||
```
|
||||
|
||||
#### func (*File) Base
|
||||
|
||||
```go
|
||||
func (fl *File) Base() int
|
||||
```
|
||||
|
||||
#### func (*File) Name
|
||||
|
||||
```go
|
||||
func (fl *File) Name() string
|
||||
```
|
||||
|
||||
#### func (*File) Source
|
||||
|
||||
```go
|
||||
func (fl *File) Source() string
|
||||
```
|
||||
|
||||
#### type FileSet
|
||||
|
||||
```go
|
||||
type FileSet struct {
|
||||
}
|
||||
```
|
||||
|
||||
A FileSet represents a set of source files.
|
||||
|
||||
#### func (*FileSet) AddFile
|
||||
|
||||
```go
|
||||
func (self *FileSet) AddFile(filename, src string) int
|
||||
```
|
||||
AddFile adds a new file with the given filename and src.
|
||||
|
||||
This an internal method, but exported for cross-package use.
|
||||
|
||||
#### func (*FileSet) File
|
||||
|
||||
```go
|
||||
func (self *FileSet) File(idx Idx) *File
|
||||
```
|
||||
|
||||
#### func (*FileSet) Position
|
||||
|
||||
```go
|
||||
func (self *FileSet) Position(idx Idx) *Position
|
||||
```
|
||||
Position converts an Idx in the FileSet into a Position.
|
||||
|
||||
#### type Idx
|
||||
|
||||
```go
|
||||
type Idx int
|
||||
```
|
||||
|
||||
Idx is a compact encoding of a source position within a file set. It can be
|
||||
converted into a Position for a more convenient, but much larger,
|
||||
representation.
|
||||
|
||||
#### type Position
|
||||
|
||||
```go
|
||||
type Position struct {
|
||||
Filename string // The filename where the error occurred, if any
|
||||
Offset int // The src offset
|
||||
Line int // The line number, starting at 1
|
||||
Column int // The column number, starting at 1 (The character count)
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Position describes an arbitrary source position including the filename, line,
|
||||
and column location.
|
||||
|
||||
#### func (*Position) String
|
||||
|
||||
```go
|
||||
func (self *Position) String() string
|
||||
```
|
||||
String returns a string in one of several forms:
|
||||
|
||||
file:line:column A valid position with filename
|
||||
line:column A valid position without filename
|
||||
file An invalid position with filename
|
||||
- An invalid position without filename
|
||||
|
||||
--
|
||||
**godocdown** http://github.com/robertkrimen/godocdown
|
||||
234
common/utils/goja/file/file.go
Normal file
234
common/utils/goja/file/file.go
Normal file
@@ -0,0 +1,234 @@
|
||||
// Package file encapsulates the file abstractions used by the ast & parser.
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/go-sourcemap/sourcemap"
|
||||
)
|
||||
|
||||
// Idx is a compact encoding of a source position within a file set.
|
||||
// It can be converted into a Position for a more convenient, but much
|
||||
// larger, representation.
|
||||
type Idx int
|
||||
|
||||
// Position describes an arbitrary source position
|
||||
// including the filename, line, and column location.
|
||||
type Position struct {
|
||||
Filename string // The filename where the error occurred, if any
|
||||
Line int // The line number, starting at 1
|
||||
Column int // The column number, starting at 1 (The character count)
|
||||
|
||||
}
|
||||
|
||||
// A Position is valid if the line number is > 0.
|
||||
|
||||
func (self *Position) isValid() bool {
|
||||
return self.Line > 0
|
||||
}
|
||||
|
||||
// String returns a string in one of several forms:
|
||||
//
|
||||
// file:line:column A valid position with filename
|
||||
// line:column A valid position without filename
|
||||
// file An invalid position with filename
|
||||
// - An invalid position without filename
|
||||
func (self Position) String() string {
|
||||
str := self.Filename
|
||||
if self.isValid() {
|
||||
if str != "" {
|
||||
str += ":"
|
||||
}
|
||||
str += fmt.Sprintf("%d:%d", self.Line, self.Column)
|
||||
}
|
||||
if str == "" {
|
||||
str = "-"
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// FileSet
|
||||
|
||||
// A FileSet represents a set of source files.
|
||||
type FileSet struct {
|
||||
files []*File
|
||||
last *File
|
||||
}
|
||||
|
||||
// AddFile adds a new file with the given filename and src.
|
||||
//
|
||||
// This an internal method, but exported for cross-package use.
|
||||
func (self *FileSet) AddFile(filename, src string) int {
|
||||
base := self.nextBase()
|
||||
file := &File{
|
||||
name: filename,
|
||||
src: src,
|
||||
base: base,
|
||||
}
|
||||
self.files = append(self.files, file)
|
||||
self.last = file
|
||||
return base
|
||||
}
|
||||
|
||||
func (self *FileSet) nextBase() int {
|
||||
if self.last == nil {
|
||||
return 1
|
||||
}
|
||||
return self.last.base + len(self.last.src) + 1
|
||||
}
|
||||
|
||||
func (self *FileSet) File(idx Idx) *File {
|
||||
for _, file := range self.files {
|
||||
if idx <= Idx(file.base+len(file.src)) {
|
||||
return file
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Position converts an Idx in the FileSet into a Position.
|
||||
func (self *FileSet) Position(idx Idx) Position {
|
||||
for _, file := range self.files {
|
||||
if idx <= Idx(file.base+len(file.src)) {
|
||||
return file.Position(int(idx) - file.base)
|
||||
}
|
||||
}
|
||||
return Position{}
|
||||
}
|
||||
|
||||
type File struct {
|
||||
mu sync.Mutex
|
||||
name string
|
||||
src string
|
||||
base int // This will always be 1 or greater
|
||||
sourceMap *sourcemap.Consumer
|
||||
lineOffsets []int
|
||||
lastScannedOffset int
|
||||
}
|
||||
|
||||
func NewFile(filename, src string, base int) *File {
|
||||
return &File{
|
||||
name: filename,
|
||||
src: src,
|
||||
base: base,
|
||||
}
|
||||
}
|
||||
|
||||
func (fl *File) Name() string {
|
||||
return fl.name
|
||||
}
|
||||
|
||||
func (fl *File) Source() string {
|
||||
return fl.src
|
||||
}
|
||||
|
||||
func (fl *File) Base() int {
|
||||
return fl.base
|
||||
}
|
||||
|
||||
func (fl *File) SetSourceMap(m *sourcemap.Consumer) {
|
||||
fl.sourceMap = m
|
||||
}
|
||||
|
||||
func (fl *File) Position(offset int) Position {
|
||||
var line int
|
||||
var lineOffsets []int
|
||||
fl.mu.Lock()
|
||||
if offset > fl.lastScannedOffset {
|
||||
line = fl.scanTo(offset)
|
||||
lineOffsets = fl.lineOffsets
|
||||
fl.mu.Unlock()
|
||||
} else {
|
||||
lineOffsets = fl.lineOffsets
|
||||
fl.mu.Unlock()
|
||||
line = sort.Search(len(lineOffsets), func(x int) bool { return lineOffsets[x] > offset }) - 1
|
||||
}
|
||||
|
||||
var lineStart int
|
||||
if line >= 0 {
|
||||
lineStart = lineOffsets[line]
|
||||
}
|
||||
|
||||
row := line + 2
|
||||
col := offset - lineStart + 1
|
||||
|
||||
if fl.sourceMap != nil {
|
||||
if source, _, row, col, ok := fl.sourceMap.Source(row, col); ok {
|
||||
sourceUrlStr := source
|
||||
sourceURL := ResolveSourcemapURL(fl.Name(), source)
|
||||
if sourceURL != nil {
|
||||
sourceUrlStr = sourceURL.String()
|
||||
}
|
||||
|
||||
return Position{
|
||||
Filename: sourceUrlStr,
|
||||
Line: row,
|
||||
Column: col,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Position{
|
||||
Filename: fl.name,
|
||||
Line: row,
|
||||
Column: col,
|
||||
}
|
||||
}
|
||||
|
||||
func ResolveSourcemapURL(basename, source string) *url.URL {
|
||||
// if the url is absolute(has scheme) there is nothing to do
|
||||
smURL, err := url.Parse(strings.TrimSpace(source))
|
||||
if err == nil && !smURL.IsAbs() {
|
||||
baseURL, err1 := url.Parse(strings.TrimSpace(basename))
|
||||
if err1 == nil && path.IsAbs(baseURL.Path) {
|
||||
smURL = baseURL.ResolveReference(smURL)
|
||||
} else {
|
||||
// pathological case where both are not absolute paths and using Resolve
|
||||
// as above will produce an absolute one
|
||||
smURL, _ = url.Parse(path.Join(path.Dir(basename), smURL.Path))
|
||||
}
|
||||
}
|
||||
return smURL
|
||||
}
|
||||
|
||||
func findNextLineStart(s string) int {
|
||||
for pos, ch := range s {
|
||||
switch ch {
|
||||
case '\r':
|
||||
if pos < len(s)-1 && s[pos+1] == '\n' {
|
||||
return pos + 2
|
||||
}
|
||||
return pos + 1
|
||||
case '\n':
|
||||
return pos + 1
|
||||
case '\u2028', '\u2029':
|
||||
return pos + 3
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func (fl *File) scanTo(offset int) int {
|
||||
o := fl.lastScannedOffset
|
||||
for o < offset {
|
||||
p := findNextLineStart(fl.src[o:])
|
||||
if p == -1 {
|
||||
fl.lastScannedOffset = len(fl.src)
|
||||
return len(fl.lineOffsets) - 1
|
||||
}
|
||||
o = o + p
|
||||
fl.lineOffsets = append(fl.lineOffsets, o)
|
||||
}
|
||||
fl.lastScannedOffset = o
|
||||
|
||||
if o == offset {
|
||||
return len(fl.lineOffsets) - 1
|
||||
}
|
||||
|
||||
return len(fl.lineOffsets) - 2
|
||||
}
|
||||
75
common/utils/goja/file/file_test.go
Normal file
75
common/utils/goja/file/file_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPosition(t *testing.T) {
|
||||
const SRC = `line1
|
||||
line2
|
||||
line3`
|
||||
f := NewFile("", SRC, 0)
|
||||
|
||||
tests := []struct {
|
||||
offset int
|
||||
line int
|
||||
col int
|
||||
}{
|
||||
{0, 1, 1},
|
||||
{2, 1, 3},
|
||||
{2, 1, 3},
|
||||
{6, 2, 1},
|
||||
{7, 2, 2},
|
||||
{12, 3, 1},
|
||||
{12, 3, 1},
|
||||
{13, 3, 2},
|
||||
{13, 3, 2},
|
||||
{16, 3, 5},
|
||||
{17, 3, 6},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
if p := f.Position(test.offset); p.Line != test.line || p.Column != test.col {
|
||||
t.Fatalf("%d. Line: %d, col: %d", i, p.Line, p.Column)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileConcurrency(t *testing.T) {
|
||||
const SRC = `line1
|
||||
line2
|
||||
line3`
|
||||
f := NewFile("", SRC, 0)
|
||||
go func() {
|
||||
f.Position(12)
|
||||
}()
|
||||
f.Position(2)
|
||||
}
|
||||
|
||||
func TestGetSourceFilename(t *testing.T) {
|
||||
tests := []struct {
|
||||
source, basename, result string
|
||||
}{
|
||||
{"test.js", "base.js", "test.js"},
|
||||
{"test.js", "../base.js", "../test.js"},
|
||||
{"test.js", "/somewhere/base.js", "/somewhere/test.js"},
|
||||
{"/test.js", "/somewhere/base.js", "/test.js"},
|
||||
{"/test.js", "file:///somewhere/base.js", "file:///test.js"},
|
||||
{"file:///test.js", "base.js", "file:///test.js"},
|
||||
{"file:///test.js", "/somwehere/base.js", "file:///test.js"},
|
||||
{"file:///test.js", "file:///somewhere/base.js", "file:///test.js"},
|
||||
{"../test.js", "/somewhere/else/base.js", "/somewhere/test.js"},
|
||||
{"../test.js", "file:///somewhere/else/base.js", "file:///somewhere/test.js"},
|
||||
{"../test.js", "https://example.com/somewhere/else/base.js", "https://example.com/somewhere/test.js"},
|
||||
{"\ntest.js", "base123.js", "test.js"},
|
||||
{"\rtest2.js\t\n ", "base123.js", "test2.js"},
|
||||
// TODO find something that won't parse
|
||||
}
|
||||
for _, test := range tests {
|
||||
resultURL := ResolveSourcemapURL(test.basename, test.source)
|
||||
result := resultURL.String()
|
||||
if result != test.result {
|
||||
t.Fatalf("source: %q, basename %q produced %q instead of %q", test.source, test.basename, result, test.result)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user