diff --git a/LICENSE b/LICENSE index f288702..0ad25db 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies @@ -7,17 +7,15 @@ Preamble - The GNU General Public License is a free, copyleft license for -software and other kinds of works. + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to +our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. +software for all its users. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you @@ -26,44 +24,34 @@ them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. The precise terms and conditions for copying, distribution and modification follow. @@ -72,7 +60,7 @@ modification follow. 0. Definitions. - "This License" refers to version 3 of the GNU General Public License. + "This License" refers to version 3 of the GNU Affero General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. @@ -549,35 +537,45 @@ to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. - 13. Use with the GNU Affero General Public License. + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single +under version 3 of the GNU General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General +Program specifies that a certain numbered version of the GNU Affero General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published +GNU Affero General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's +versions of the GNU Affero General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. @@ -635,40 +633,29 @@ the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + GNU Affero General Public License for more details. - You should have received a copy of the GNU General Public License + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see +For more information on this, and how to apply and follow the GNU AGPL, see . - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. diff --git a/analyzer.go b/analyzer.go index 5686fe1..b1c16d5 100644 --- a/analyzer.go +++ b/analyzer.go @@ -1,8 +1,10 @@ - package mcla import ( + "context" "errors" + "io" + "sync" ) type SolutionPossibility struct { @@ -10,6 +12,12 @@ type SolutionPossibility struct { Match float32 `json:"match"` } +type ErrorResult struct { + Error *JavaError `json:"error"` + Matched []SolutionPossibility `json:"matched"` + File string `json:"file,omitempty"` +} + var ( ErrCrashReportIncomplete = errors.New("Crashreport is incomplete") ) @@ -18,16 +26,16 @@ type Analyzer struct { DB ErrorDB } -func NewAnalyzer(db ErrorDB)(a *Analyzer){ +func NewAnalyzer(db ErrorDB) (a *Analyzer) { return &Analyzer{ DB: db, } } -func (a *Analyzer)DoError(jerr *JavaError)(matched []SolutionPossibility, err error){ +func (a *Analyzer) DoError(jerr *JavaError) (matched []SolutionPossibility, err error) { for jerr != nil { epkg, ecls := rsplit(jerr.Class, '.') - a.DB.ForEachErrors(func(e *ErrorDesc)(err error){ + a.DB.ForEachErrors(func(e *ErrorDesc) (err error) { sol := SolutionPossibility{ ErrorDesc: e, } @@ -36,18 +44,18 @@ func (a *Analyzer)DoError(jerr *JavaError)(matched []SolutionPossibility, err er if !ignoreErrorTyp && ecls2 == ecls { // error type weight: 10% if epkg2 == "*" || epkg == epkg2 { sol.Match = 0.1 // 10% - }else{ + } else { sol.Match = 0.05 // 5% } } if len(e.Message) == 0 { // when ignore error message, error type provide 100% score weight sol.Match /= 10 / 100 - }else{ + } else { jemsg, _ := split(jerr.Message, '\n') matches := lineMatchPercent(jemsg, e.Message) // error message weight: 90% if ignoreErrorTyp { sol.Match = matches // or when ignore error type, it provide 100% score weight - }else{ + } else { sol.Match += matches * 0.9 } } @@ -63,3 +71,45 @@ func (a *Analyzer)DoError(jerr *JavaError)(matched []SolutionPossibility, err er } return } + +func (a *Analyzer) DoLogStream(c context.Context, r io.Reader) (<-chan *ErrorResult, context.Context) { + result := make(chan *ErrorResult, 3) + ctx, cancel := context.WithCancelCause(c) + go func() { + defer close(result) + var wg sync.WaitGroup + resCh, errCh := ScanJavaErrorsIntoChan(r) + LOOP: + for { + select { + case jerr := <-resCh: + if jerr == nil { + break LOOP + } + wg.Add(1) + go func() { + defer wg.Done() + res := &ErrorResult{ + Error: jerr, + } + var err error + if res.Matched, err = a.DoError(jerr); err != nil { + cancel(err) + return + } + select { + case result <- res: + case <-ctx.Done(): + } + }() + case err := <-errCh: + cancel(err) + return + case <-ctx.Done(): + return + } + } + wg.Wait() + }() + return result, ctx +} diff --git a/cmds/mcla/analysis.go b/cmds/mcla/analysis.go deleted file mode 100644 index fd17234..0000000 --- a/cmds/mcla/analysis.go +++ /dev/null @@ -1,52 +0,0 @@ - -package main - -import ( - "io" - "sync" - - "github.com/GlobeMC/mcla" -) - -type ErrorResult struct { - Error *mcla.JavaError `json:"error"` - Matched []mcla.SolutionPossibility `json:"matched"` - File string `json:"file"` -} - -func analyzeLogErrors(r io.Reader)(<-chan *ErrorResult, <-chan error){ - resCh := make(chan *ErrorResult, 3) - errCh := make(chan error, 1) - go func(){ - defer close(resCh) - var wg sync.WaitGroup - jerrCh, errC := mcla.ScanJavaErrorsIntoChan(r) - LOOP: - for { - select { - case err := <-errC: - errCh <- err - return - case jerr := <-jerrCh: - if jerr == nil { - break LOOP - } - wg.Add(1) - go func(){ - defer wg.Done() - var err error - res := &ErrorResult{ - Error: jerr, - } - if res.Matched, err = defaultAnalyzer.DoError(jerr); err != nil { - errCh <- err - return - } - resCh <- res - }() - } - } - wg.Wait() - }() - return resCh, errCh -} diff --git a/cmds/mcla/errdb.go b/cmds/mcla/errdb.go index ecfbeae..c88459d 100644 --- a/cmds/mcla/errdb.go +++ b/cmds/mcla/errdb.go @@ -1,197 +1,42 @@ - package main import ( - "encoding/json" "fmt" + "io" "net/http" "net/url" - "sync" - "sync/atomic" - "time" "github.com/GlobeMC/mcla" + "github.com/GlobeMC/mcla/ghdb" ) -const syntaxVersion = 0 // 0 means dev - -type UnsupportSyntaxErr struct { - Version int -} - -func (e *UnsupportSyntaxErr)Error()(string){ - return fmt.Sprintf("MCLA-DB syntax version %d is not supported, please update the application", e.Version) -} - type HTTPStatusErr struct { URL string StatusCode int } -func (e *HTTPStatusErr)Error()(string){ +func (e *HTTPStatusErr) Error() string { return fmt.Sprintf("HTTP status code error: %d when getting %q", e.StatusCode, e.URL) } -type versionDataT struct { - Major int `json:"major"` - Minor int `json:"minor"` - Patch int `json:"patch"` - ErrorIncId int `json:"errorIncId"` - SolutionIncId int `json:"solutionIncId"` -} - -type ghErrDB struct { - Prefix string - checking atomic.Bool - cachedVersion versionDataT - lastCheck time.Time -} - -var _ mcla.ErrorDB = (*ghErrDB)(nil) - -func (db *ghErrDB)fetch(subpaths ...string)(res *http.Response, err error){ - var path string - if path, err = url.JoinPath(db.Prefix, subpaths...); err != nil { - return - } - if res, err = http.DefaultClient.Get(path); err != nil { - return - } - return -} - -func (db *ghErrDB)fetchGhDBVersion()(v versionDataT, err error){ - var res *http.Response - if res, err = db.fetch("version.json"); err != nil { - return - } - defer res.Body.Close() - if res.StatusCode != 200 { - err = &HTTPStatusErr{ res.Request.URL.String(), res.StatusCode } - return - } - if err = json.NewDecoder(res.Body).Decode(&v); err != nil { - return - } - if v.Major != syntaxVersion { - err = &UnsupportSyntaxErr{ v.Major } - return - } - return -} +var ghRepoPrefix = "https://raw.githubusercontent.com/kmcsr/mcla-db-dev/main" -func (db *ghErrDB)getErrorDesc(id int)(desc *mcla.ErrorDesc, err error){ - // if getStorageValue(fmt.Sprintf("gh.db.errs.%d", id), &desc) { - // return - // } - res, err := db.fetch("errors", fmt.Sprintf("%d.json", id)) - if err != nil { - return - } - if res.StatusCode != 200 { - res.Body.Close() - if res.StatusCode == 404 { - return +var defaultErrDB = &ghdb.ErrDB{ + Fetch: func(path string) (io.ReadCloser, error) { + path, err := url.JoinPath(ghRepoPrefix, path) + if err != nil { + return nil, err } - return nil, &HTTPStatusErr{ res.Request.URL.String(), res.StatusCode } - } - if err = json.NewDecoder(res.Body).Decode(&desc); err != nil { - return - } - return -} - -func (db *ghErrDB)checkUpdate()(err error){ - if !db.checking.CompareAndSwap(false, true) { - return - } - defer db.checking.Store(false) - - if !db.lastCheck.IsZero() && time.Since(db.lastCheck) <= time.Minute { - return - } - - newVersion, err := db.fetchGhDBVersion() - if err != nil { - return - } - // TODO: do we really need caches? - if newVersion.Major != db.cachedVersion.Major || newVersion.Minor != db.cachedVersion.Minor { - // TODO: refresh all data - db.cachedVersion = newVersion - }else if newVersion.Patch != db.cachedVersion.Patch { - var wg sync.WaitGroup - wg.Add(newVersion.ErrorIncId - db.cachedVersion.ErrorIncId) - for i := db.cachedVersion.ErrorIncId + 1; i <= newVersion.ErrorIncId; i++ { - go func(i int){ - defer wg.Done() - var ( - res *http.Response - err error - ) - if res, err = db.fetch("errors", fmt.Sprintf("%d.json", i)); err != nil { - return - } - // stokey := fmt.Sprintf("gh.db.errs.%d", i) - if res.StatusCode != 200 { - res.Body.Close() - // setStorageValue(stokey, nil) - return - } - var v *mcla.ErrorDesc - e := json.NewDecoder(res.Body).Decode(&v) - res.Body.Close() - if e != nil { - // delStorageValue(stokey) - return - } - // setStorageValue(stokey, v) - }(i) + res, err := http.DefaultClient.Get(path) + if err != nil { + return nil, err } - wg.Wait() - db.cachedVersion.ErrorIncId = newVersion.ErrorIncId - for i := db.cachedVersion.SolutionIncId + 1; i <= newVersion.SolutionIncId; i++ { - // var res *Response - // if res, err = db.fetch("solutions", fmt.Sprintf("%d.json", i)); res.StatusCode != 200 { - // res.Body.Close() - // if res.StatusCode == 404 { - // continue - // } - // return &HTTPStatusErr{ res.Url, res.StatusCode } - // } + if res.StatusCode != 200 { + res.Body.Close() + return nil, &HTTPStatusErr{res.Request.URL.String(), res.StatusCode} } - db.cachedVersion.SolutionIncId = newVersion.SolutionIncId - db.cachedVersion.Patch = newVersion.Patch - } - // setStorageValue("gh.db.version", db.cachedVersion) - - db.lastCheck = time.Now() - return -} - -func (db *ghErrDB)ForEachErrors(callback func(*mcla.ErrorDesc)(error))(err error){ - if err = db.checkUpdate(); err != nil { - return - } - for i := 1; i <= db.cachedVersion.ErrorIncId; i++ { - var desc *mcla.ErrorDesc - if desc, err = db.getErrorDesc(i); err != nil { - return - } - if err = callback(desc); err != nil { - return - } - } - return -} - -func (*ghErrDB)GetSolution(id int)(*mcla.SolutionDesc, error){ - panic("Unimplemented operation 'GetSolution'") - return nil, nil -} - -var defaultErrDB = &ghErrDB{ - Prefix: "https://raw.githubusercontent.com/kmcsr/mcla-db-dev/main", + return res.Body, nil + }, } var defaultAnalyzer = &mcla.Analyzer{ diff --git a/cmds/mcla/help.go b/cmds/mcla/help.go index 979556c..08fb9d5 100644 --- a/cmds/mcla/help.go +++ b/cmds/mcla/help.go @@ -1,4 +1,3 @@ - package main import ( @@ -19,7 +18,7 @@ Subcommands: - analyzeErrors [...] ` -func help(){ +func help() { fmt.Printf(LICENSE, version) fmt.Print(HELP_MESSAGE) } diff --git a/cmds/mcla/main.go b/cmds/mcla/main.go index d83063f..b05b98f 100644 --- a/cmds/mcla/main.go +++ b/cmds/mcla/main.go @@ -1,7 +1,7 @@ - package main import ( + "context" "encoding/json" "fmt" "os" @@ -9,14 +9,14 @@ import ( "github.com/GlobeMC/mcla" ) -func printf(format string, args ...any){ - if format[len(format) - 1] != '\n' { +func printf(format string, args ...any) { + if format[len(format)-1] != '\n' { format += "\n" } fmt.Fprintf(os.Stderr, format, args...) } -func main(){ +func main() { if len(os.Args) <= 1 { help() return @@ -64,14 +64,14 @@ func main(){ } } -func analysisAndOutput(file string){ +func analysisAndOutput(file string) { fd, err := os.Open(file) if err != nil { printf("Error when opening file %q: %v", file, err) os.Exit(1) } defer fd.Close() - resCh, errCh := analyzeLogErrors(fd) + result, ctx := defaultAnalyzer.DoLogStream(context.Background(), fd) encoder := json.NewEncoder(os.Stdout) encoder.SetEscapeHTML(false) encoder.SetIndent("", " ") @@ -79,7 +79,7 @@ func analysisAndOutput(file string){ LOOP_RES: for { select { - case res := <-resCh: + case res := <-result: if res == nil { // done break LOOP_RES } @@ -89,7 +89,8 @@ LOOP_RES: printf("\nError when encoding report file as json: %v", err) os.Exit(1) } - case err := <-errCh: + case <-ctx.Done(): + err := context.Cause(ctx) printf("Error when analyzing file %q: %v", file, err) os.Exit(1) } diff --git a/cmds/mcla/version.go b/cmds/mcla/version.go index c9dec69..f15a3fa 100644 --- a/cmds/mcla/version.go +++ b/cmds/mcla/version.go @@ -1,9 +1,8 @@ - package main var version string -func init(){ +func init() { if version == "" { version = "dev" } diff --git a/cmds/mcla_wasm/errdb.go b/cmds/mcla_wasm/errdb.go index cad579b..81019ba 100644 --- a/cmds/mcla_wasm/errdb.go +++ b/cmds/mcla_wasm/errdb.go @@ -1,194 +1,43 @@ - package main import ( - "encoding/json" "fmt" + "io" "net/url" - "sync" - "sync/atomic" - "time" - . "github.com/GlobeMC/mcla" + "github.com/GlobeMC/mcla" + "github.com/GlobeMC/mcla/ghdb" ) -const syntaxVersion = 0 // 0 means dev - -type UnsupportSyntaxErr struct { - Version int -} - -func (e *UnsupportSyntaxErr)Error()(string){ - return fmt.Sprintf("MCLA-DB syntax version %d is not supported, please update the application", e.Version) -} - type HTTPStatusErr struct { URL string StatusCode int } -func (e *HTTPStatusErr)Error()(string){ +func (e *HTTPStatusErr) Error() string { return fmt.Sprintf("HTTP status code error: %d when getting %q", e.StatusCode, e.URL) } -type versionDataT struct { - Major int `json:"major"` - Minor int `json:"minor"` - Patch int `json:"patch"` - ErrorIncId int `json:"errorIncId"` - SolutionIncId int `json:"solutionIncId"` -} - -type ghErrDB struct { - Prefix string - checking atomic.Bool - cachedVersion versionDataT - lastCheck time.Time -} - -var _ ErrorDB = (*ghErrDB)(nil) - -func (db *ghErrDB)fetch(subpaths ...string)(res *Response, err error){ - var path string - if path, err = url.JoinPath(db.Prefix, subpaths...); err != nil { - return - } - if res, err = fetch(path); err != nil { - return - } - return -} - -func (db *ghErrDB)fetchGhDBVersion()(v versionDataT, err error){ - var res *Response - if res, err = db.fetch("version.json"); err != nil { - return - } - defer res.Body.Close() - if res.StatusCode != 200 { - err = &HTTPStatusErr{ res.Url, res.StatusCode } - return - } - if err = json.NewDecoder(res.Body).Decode(&v); err != nil { - return - } - if v.Major != syntaxVersion { - err = &UnsupportSyntaxErr{ v.Major } - return - } - return -} +var ghRepoPrefix = "https://raw.githubusercontent.com/kmcsr/mcla-db-dev/main" -func (db *ghErrDB)getErrorDesc(id int)(desc *ErrorDesc, err error){ - if getStorageValue(fmt.Sprintf("gh.db.errs.%d", id), &desc) { - return - } - res, err := db.fetch("errors", fmt.Sprintf("%d.json", id)) - if res.StatusCode != 200 { - res.Body.Close() - if res.StatusCode == 404 { - return +var defaultErrDB = &ghdb.ErrDB{ + Fetch: func(path string) (io.ReadCloser, error) { + path, err := url.JoinPath(ghRepoPrefix, path) + if err != nil { + return nil, err } - return nil, &HTTPStatusErr{ res.Url, res.StatusCode } - } - if err = json.NewDecoder(res.Body).Decode(&desc); err != nil { - return - } - return -} - -func (db *ghErrDB)checkUpdate()(err error){ - if !db.checking.CompareAndSwap(false, true) { - return - } - defer db.checking.Store(false) - - if !db.lastCheck.IsZero() && time.Since(db.lastCheck) <= time.Minute { - return nil - } - - newVersion, err := db.fetchGhDBVersion() - if err != nil { - return - } - // TODO: do we really need caches? - if newVersion.Major != db.cachedVersion.Major || newVersion.Minor != db.cachedVersion.Minor { - // TODO: refresh all data - db.cachedVersion = newVersion - }else if newVersion.Patch != db.cachedVersion.Patch { - var wg sync.WaitGroup - wg.Add(newVersion.ErrorIncId - db.cachedVersion.ErrorIncId) - for i := db.cachedVersion.ErrorIncId + 1; i <= newVersion.ErrorIncId; i++ { - go func(i int){ - defer wg.Done() - var ( - res *Response - err error - ) - if res, err = db.fetch("errors", fmt.Sprintf("%d.json", i)); err != nil { - return - } - defer res.Body.Close() - stokey := fmt.Sprintf("gh.db.errs.%d", i) - if res.StatusCode != 200 { - setStorageValue(stokey, nil) - return - } - var v *ErrorDesc - e := json.NewDecoder(res.Body).Decode(&v) - if e != nil { - delStorageValue(stokey) - return - } - setStorageValue(stokey, v) - }(i) + res, err := fetch(path) + if err != nil { + return nil, err } - wg.Wait() - db.cachedVersion.ErrorIncId = newVersion.ErrorIncId - for i := db.cachedVersion.SolutionIncId + 1; i <= newVersion.SolutionIncId; i++ { - // var res *Response - // if res, err = db.fetch("solutions", fmt.Sprintf("%d.json", i)); res.StatusCode != 200 { - // res.Body.Close() - // if res.StatusCode == 404 { - // continue - // } - // return &HTTPStatusErr{ res.Url, res.StatusCode } - // } + if res.StatusCode != 200 { + res.Body.Close() + return nil, &HTTPStatusErr{res.Url, res.StatusCode} } - db.cachedVersion.SolutionIncId = newVersion.SolutionIncId - db.cachedVersion.Patch = newVersion.Patch - } - setStorageValue("gh.db.version", db.cachedVersion) - - db.lastCheck = time.Now() - return -} - -func (db *ghErrDB)ForEachErrors(callback func(*ErrorDesc)(error))(err error){ - if err = db.checkUpdate(); err != nil { - return - } - for i := 1; i <= db.cachedVersion.ErrorIncId; i++ { - var desc *ErrorDesc - if desc, err = db.getErrorDesc(i); err != nil { - return - } - if err = callback(desc); err != nil { - return - } - } - return -} - -func (*ghErrDB)GetSolution(id int)(*SolutionDesc, error){ - panic("Unimplemented operation 'GetSolution'") - return nil, nil -} - -var defaultErrDB = &ghErrDB{ - Prefix: "https://raw.githubusercontent.com/kmcsr/mcla-db-dev/main", + return res.Body, nil + }, } -var defaultAnalyzer = &Analyzer{ +var defaultAnalyzer = &mcla.Analyzer{ DB: defaultErrDB, } diff --git a/cmds/mcla_wasm/fetch.go b/cmds/mcla_wasm/fetch.go index edeeb53..68bd7e9 100644 --- a/cmds/mcla_wasm/fetch.go +++ b/cmds/mcla_wasm/fetch.go @@ -1,4 +1,3 @@ - package main import ( @@ -20,7 +19,7 @@ type Response struct { Header Header } -func fetchContext(ctx context.Context, url string, opts ...Map)(res *Response, err error){ +func fetchContext(ctx context.Context, url string, opts ...Map) (res *Response, err error) { args := make([]any, 1, 2) args[0] = url if len(opts) > 0 { @@ -32,22 +31,22 @@ func fetchContext(ctx context.Context, url string, opts ...Map)(res *Response, e } header0 := res0.Get("headers") header := make(Header, 10) - foreachJsIterator(header0.Call("entries"), func(v js.Value)(err error){ + foreachJsIterator(header0.Call("entries"), func(v js.Value) (err error) { header.Set(v.Index(0).String(), v.Index(1).String()) return }) res = &Response{ - native: res0, - Status: res0.Get("statusText").String(), + native: res0, + Status: res0.Get("statusText").String(), StatusCode: res0.Get("status").Int(), - Body: readCloser{wrapJsValueAsReader(res0.Get("body"))}, - Type: res0.Get("type").String(), - Url: res0.Get("url").String(), - Header: header, + Body: readCloser{wrapJsValueAsReader(res0.Get("body"))}, + Type: res0.Get("type").String(), + Url: res0.Get("url").String(), + Header: header, } return } -func fetch(url string, opts ...Map)(res *Response, err error){ +func fetch(url string, opts ...Map) (res *Response, err error) { return fetchContext(bgCtx, url, opts...) } diff --git a/cmds/mcla_wasm/js_globals.go b/cmds/mcla_wasm/js_globals.go index 9ba7f10..40723db 100644 --- a/cmds/mcla_wasm/js_globals.go +++ b/cmds/mcla_wasm/js_globals.go @@ -1,4 +1,3 @@ - package main import ( @@ -9,10 +8,10 @@ import ( var ( global = js.Global() // class - Object = global.Get("Object") - Promise = global.Get("Promise") - Uint8Array = global.Get("Uint8Array") - ReadableStream = global.Get("ReadableStream") + Object = global.Get("Object") + Promise = global.Get("Promise") + Uint8Array = global.Get("Uint8Array") + ReadableStream = global.Get("ReadableStream") ReadableStreamDefaultReader = global.Get("ReadableStreamDefaultReader") ReadableStreamBYOBReader = global.Get("ReadableStreamBYOBReader") // function @@ -26,7 +25,7 @@ var ( const appStorageKeyPrefix = "com.github.kmcsr.mcla." // TODO: use https://developer.mozilla.org/en-US/docs/Web/API/IDBFactory -func getStorageValue(key string, ptr any)(ok bool){ +func getStorageValue(key string, ptr any) (ok bool) { if !localStorage.Truthy() { return } @@ -46,7 +45,7 @@ func getStorageValue(key string, ptr any)(ok bool){ return true } -func setStorageValue(key string, value any){ +func setStorageValue(key string, value any) { if !localStorage.Truthy() { return } @@ -58,7 +57,7 @@ func setStorageValue(key string, value any){ localStorage.Call("setItem", key, (string)(buf)) } -func delStorageValue(key string){ +func delStorageValue(key string) { if !localStorage.Truthy() { return } diff --git a/cmds/mcla_wasm/main.go b/cmds/mcla_wasm/main.go index 9465e48..cc94263 100644 --- a/cmds/mcla_wasm/main.go +++ b/cmds/mcla_wasm/main.go @@ -1,12 +1,11 @@ - //go:build tinygo.wasm + package main import ( "context" "fmt" "io" - "sync" "syscall/js" . "github.com/GlobeMC/mcla" @@ -15,41 +14,41 @@ import ( var bgCtx context.Context = createBackgroundCtx() var releaseBgCtx context.CancelFunc -func createBackgroundCtx()(bgCtx context.Context){ +func createBackgroundCtx() (bgCtx context.Context) { bgCtx, releaseBgCtx = context.WithCancel(context.Background()) return } -func getAPI()(m Map){ +func getAPI() (m Map) { return Map{ "version": version, - "parseCrashReport": asyncFuncOf(func(_ js.Value, args []js.Value)(res any){ + "parseCrashReport": asyncFuncOf(func(_ js.Value, args []js.Value) (res any) { return asJsValue(parseCrashReport(args)) }), - "parseLogErrors": asyncFuncOf(func(_ js.Value, args []js.Value)(res any){ + "parseLogErrors": asyncFuncOf(func(_ js.Value, args []js.Value) (res any) { return asJsValue(parseLogErrors(args)) }), - "analyzeLogErrors": asyncFuncOf(func(_ js.Value, args []js.Value)(res any){ + "analyzeLogErrors": asyncFuncOf(func(_ js.Value, args []js.Value) (res any) { return analyzeLogErrors(args) }), - "analyzeLogErrorsIter": asyncFuncOf(func(_ js.Value, args []js.Value)(res any){ + "analyzeLogErrorsIter": asyncFuncOf(func(_ js.Value, args []js.Value) (res any) { return analyzeLogErrorsIter(args) }), - "setGhDbPrefix": js.FuncOf(func(_ js.Value, args []js.Value)(res any){ + "setGhDbPrefix": js.FuncOf(func(_ js.Value, args []js.Value) (res any) { prefix := args[0] prefixStr := prefix.String() fmt.Printf("Set database as %q\n", prefixStr) - defaultErrDB.Prefix = prefixStr + ghRepoPrefix = prefixStr return }), } } -func main(){ - defaultErrDB.checkUpdate() // refresh caches +func main() { + defaultErrDB.RefreshCache() api := getAPI() - api["release"] = js.FuncOf(func(_ js.Value, _ []js.Value)(_ any){ + api["release"] = js.FuncOf(func(_ js.Value, _ []js.Value) (_ any) { global.Delete("MCLA") releaseBgCtx() return js.Undefined() @@ -62,7 +61,7 @@ func main(){ <-bgCtx.Done() } -func parseCrashReport(args []js.Value)(report *CrashReport){ +func parseCrashReport(args []js.Value) (report *CrashReport) { value := args[0] r := wrapJsValueAsReader(value) var err error @@ -75,7 +74,7 @@ func parseCrashReport(args []js.Value)(report *CrashReport){ return } -func parseLogErrors(args []js.Value)(errs []*JavaError){ +func parseLogErrors(args []js.Value) (errs []*JavaError) { value := args[0] r := wrapJsValueAsReader(value) var err error @@ -85,42 +84,14 @@ func parseLogErrors(args []js.Value)(errs []*JavaError){ return } -type ErrorResult struct { - Error *JavaError `json:"error"` - Matched []SolutionPossibility `json:"matched"` -} - -func analyzeLogErrors(args []js.Value)(result []ErrorResult){ - value := args[0] - r := wrapJsValueAsReader(value) - errs, err := ScanJavaErrors(r) - if err != nil { - panic(err) - } - result = make([]ErrorResult, len(errs)) - - ctx, cancel := context.WithCancelCause(bgCtx) - doneCh := make(chan struct{}, len(errs)) - for i, jerr := range errs { - go func(){ - defer func(){ - doneCh <- struct{}{} - }() - var ( - res ErrorResult - err error - ) - res.Error = jerr - if res.Matched, err = defaultAnalyzer.DoError(jerr); err != nil { - cancel(err) - return - } - result[i] = res - }() - } - for i := 0; i < len(errs); i++ { +func analyzeLogErrors(args []js.Value) (result []*ErrorResult) { + r := wrapJsValueAsReader(args[0]) + result = make([]*ErrorResult, 0, 5) + resCh, ctx := defaultAnalyzer.DoLogStream(bgCtx, r) + for { select { - case <-doneCh: + case res := <-resCh: + result = append(result, res) case <-ctx.Done(): panic(context.Cause(ctx)) } @@ -128,45 +99,9 @@ func analyzeLogErrors(args []js.Value)(result []ErrorResult){ return } -func analyzeLogErrorsIter(args []js.Value)(iterator js.Value){ - value := args[0] - r := wrapJsValueAsReader(value) - result := make(chan *ErrorResult, 3) - ctx, cancel := context.WithCancelCause(bgCtx) +func analyzeLogErrorsIter(args []js.Value) (iterator js.Value) { + r := wrapJsValueAsReader(args[0]) + result, ctx := defaultAnalyzer.DoLogStream(bgCtx, r) iterator = NewChannelIteratorContext(ctx, result) - go func(){ - defer close(result) - var wg sync.WaitGroup - resCh, errCh := ScanJavaErrorsIntoChan(r) - LOOP: - for { - select{ - case jerr := <-resCh: - if jerr == nil { - break LOOP - } - wg.Add(1) - go func(){ - defer wg.Done() - var err error - res := &ErrorResult{ - Error: jerr, - } - if res.Matched, err = defaultAnalyzer.DoError(jerr); err != nil { - cancel(err) - return - } - select { - case result <- res: - case <-ctx.Done(): - } - }() - case err := <-errCh: - cancel(err) - return - } - } - wg.Wait() - }() return } diff --git a/cmds/mcla_wasm/main_test.go b/cmds/mcla_wasm/main_test.go index 6c81c4a..06f5c7d 100644 --- a/cmds/mcla_wasm/main_test.go +++ b/cmds/mcla_wasm/main_test.go @@ -1,4 +1,3 @@ - // this file just for keeping `tinygo test` works package main_test diff --git a/cmds/mcla_wasm/utils.go b/cmds/mcla_wasm/utils.go index e5e47c3..44b2df5 100644 --- a/cmds/mcla_wasm/utils.go +++ b/cmds/mcla_wasm/utils.go @@ -1,5 +1,5 @@ - //go:build tinygo.wasm + package main import ( @@ -13,7 +13,7 @@ import ( type Map = map[string]any -func asJsValue(v any)(res js.Value){ +func asJsValue(v any) (res js.Value) { if v == nil { return js.Null() } @@ -42,8 +42,8 @@ func asJsValue(v any)(res js.Value){ return js.ValueOf(v1) } -var GoChannelIterator = (func()(cls js.Value){ - cls = js.ValueOf(js.FuncOf(func(this js.Value, args []js.Value)(res any){ +var GoChannelIterator = (func() (cls js.Value) { + cls = js.ValueOf(js.FuncOf(func(this js.Value, args []js.Value) (res any) { return })) cls.Set("name", "GoChannelIterator") @@ -51,23 +51,23 @@ var GoChannelIterator = (func()(cls js.Value){ return })() -var _emptyIterNextFn = (func()(js.Func){ - cb := js.FuncOf(func(_ js.Value, args []js.Value)(res any){ +var _emptyIterNextFn = (func() js.Func { + cb := js.FuncOf(func(_ js.Value, args []js.Value) (res any) { resolve := args[0] - resolve.Invoke(Map{ "done": true, "value": nil }) + resolve.Invoke(Map{"done": true, "value": nil}) return }) emptyIterNextPromise := Promise.New(cb) cb.Release() - return js.FuncOf(func(_ js.Value, args []js.Value)(res any){ + return js.FuncOf(func(_ js.Value, args []js.Value) (res any) { return emptyIterNextPromise }) })() -func NewChannelIteratorContext[T any](ctx context.Context, ch <-chan T)(iter js.Value){ +func NewChannelIteratorContext[T any](ctx context.Context, ch <-chan T) (iter js.Value) { iter = GoChannelIterator.New() var nextMethod js.Func - nextMethod = asyncFuncOf(func(this js.Value, args []js.Value)(res any){ + nextMethod = asyncFuncOf(func(this js.Value, args []js.Value) (res any) { select { case <-ctx.Done(): panic(context.Cause(ctx)) @@ -75,9 +75,9 @@ func NewChannelIteratorContext[T any](ctx context.Context, ch <-chan T)(iter js. if !ok { iter.Set("next", _emptyIterNextFn) nextMethod.Release() - return Map{ "done": true, "value": nil } + return Map{"done": true, "value": nil} } - return Map{ "done": false, "value": val } + return Map{"done": false, "value": val} } }) iter.Set("next", nextMethod) @@ -90,7 +90,7 @@ type readCloser struct { var _ io.ReadCloser = readCloser{} -func (r readCloser)Close()(err error){ +func (r readCloser) Close() (err error) { if c, ok := r.Reader.(io.Closer); ok { return c.Close() } @@ -113,10 +113,11 @@ type ( var _ io.ReaderAt = uint8ArrayReader{} var _ io.ReadCloser = readableStreamDefaultReaderWrapper{} + // var _ io.Reader = readableStreamBYOBReaderWrapper{} // TODO if necessary -func (r uint8ArrayReader)ReadAt(buf []byte, offset int64)(n int, err error){ - sub := r.value.Call("subarray", (int)(offset), (int)(offset) + len(buf)) +func (r uint8ArrayReader) ReadAt(buf []byte, offset int64) (n int, err error) { + sub := r.value.Call("subarray", (int)(offset), (int)(offset)+len(buf)) n = js.CopyBytesToGo(buf, sub) if n != len(buf) { err = io.EOF @@ -124,7 +125,7 @@ func (r uint8ArrayReader)ReadAt(buf []byte, offset int64)(n int, err error){ return } -func (r readableStreamDefaultReaderWrapper)readFromInternalBuf(buf []byte)(n int, err error){ +func (r readableStreamDefaultReaderWrapper) readFromInternalBuf(buf []byte) (n int, err error) { if r.buf != nil { n, err = r.buf.ReadAt(buf, (int64)(r.off)) r.off += n @@ -136,7 +137,7 @@ func (r readableStreamDefaultReaderWrapper)readFromInternalBuf(buf []byte)(n int return } -func (r readableStreamDefaultReaderWrapper)Read(buf []byte)(n int, err error){ +func (r readableStreamDefaultReaderWrapper) Read(buf []byte) (n int, err error) { if len(buf) == 0 { return } @@ -154,25 +155,25 @@ func (r readableStreamDefaultReaderWrapper)Read(buf []byte)(n int, err error){ return r.readFromInternalBuf(buf) } -func (r readableStreamDefaultReaderWrapper)Close()(err error){ +func (r readableStreamDefaultReaderWrapper) Close() (err error) { awaitPromise(r.value.Call("cancel")) r.value.Call("releaseLock") return } -func wrapJsValueAsReader(value js.Value)(r io.Reader){ +func wrapJsValueAsReader(value js.Value) (r io.Reader) { switch value.Type() { case js.TypeString: return strings.NewReader(value.String()) case js.TypeObject: if value.InstanceOf(Uint8Array) { - return io.NewSectionReader(uint8ArrayReader{value}, 0, (1 << 63) - 1) + return io.NewSectionReader(uint8ArrayReader{value}, 0, (1<<63)-1) } if value.InstanceOf(ReadableStream) { - value = value.Call("getReader"/*, Map{ "mode": "byob" } TODO*/) + value = value.Call("getReader" /*, Map{ "mode": "byob" } TODO*/) } if value.InstanceOf(ReadableStreamDefaultReader) { - return readableStreamDefaultReaderWrapper{ value: value } + return readableStreamDefaultReaderWrapper{value: value} } // if value.InstanceOf(ReadableStreamBYOBReader) { // TODO // return readableStreamBYOBReaderWrapper{ value } @@ -183,16 +184,20 @@ func wrapJsValueAsReader(value js.Value)(r io.Reader){ } // have to ensure the argument is a really JS Promise instance -func wrapPromise(promise js.Value)(done <-chan js.Value, err <-chan error){ +func wrapPromise(promise js.Value) (done <-chan js.Value, err <-chan error) { done0 := make(chan js.Value, 1) err0 := make(chan error, 1) - var success, failed js.Func - success = js.FuncOf(func(_ js.Value, args []js.Value)(res any){ + var success, failed js.Func + success = js.FuncOf(func(_ js.Value, args []js.Value) (res any) { + success.Release() + failed.Release() done0 <- args[0] return }) - failed = js.FuncOf(func(_ js.Value, args []js.Value)(res any){ + failed = js.FuncOf(func(_ js.Value, args []js.Value) (res any) { + success.Release() + failed.Release() err0 <- js.Error{args[0]} return }) @@ -200,7 +205,7 @@ func wrapPromise(promise js.Value)(done <-chan js.Value, err <-chan error){ return done0, err0 } -func awaitPromiseContext(ctx context.Context, promise js.Value)(res js.Value, err error){ +func awaitPromiseContext(ctx context.Context, promise js.Value) (res js.Value, err error) { if promise.Type() != js.TypeObject || !promise.InstanceOf(Promise) { return promise, nil } @@ -216,27 +221,27 @@ func awaitPromiseContext(ctx context.Context, promise js.Value)(res js.Value, er } } -func awaitPromise(promise js.Value)(res js.Value, err error){ +func awaitPromise(promise js.Value) (res js.Value, err error) { return awaitPromiseContext(bgCtx, promise) } -func asyncFuncOf(fn func(this js.Value, args []js.Value)(res any))(js.Func){ - return js.FuncOf(func(this js.Value, args []js.Value)(res any){ +func asyncFuncOf(fn func(this js.Value, args []js.Value) (res any)) js.Func { + return js.FuncOf(func(this js.Value, args []js.Value) (res any) { var resolve, reject js.Value - pcb := js.FuncOf(func(_ js.Value, args []js.Value)(res any){ + pcb := js.FuncOf(func(_ js.Value, args []js.Value) (res any) { resolve, reject = args[0], args[1] return }) res = Promise.New(pcb) pcb.Release() - go func(){ - defer func(){ + go func() { + defer func() { if e := recover(); e != nil { if je, ok := e.(js.Error); ok { reject.Invoke(je.Value) - }else if er, ok := e.(error); ok { + } else if er, ok := e.(error); ok { reject.Invoke(er.Error()) - }else{ + } else { reject.Invoke(asJsValue(e)) } } @@ -248,7 +253,7 @@ func asyncFuncOf(fn func(this js.Value, args []js.Value)(res any))(js.Func){ }) } -func foreachJsIterator(iterator js.Value, callback func(js.Value)(error))(err error){ +func foreachJsIterator(iterator js.Value, callback func(js.Value) error) (err error) { for { res := iterator.Call("next") if !res.Get("done").Bool() { diff --git a/cmds/mcla_wasm/version.go b/cmds/mcla_wasm/version.go index c9dec69..f15a3fa 100644 --- a/cmds/mcla_wasm/version.go +++ b/cmds/mcla_wasm/version.go @@ -1,9 +1,8 @@ - package main var version string -func init(){ +func init() { if version == "" { version = "dev" } diff --git a/crashreport.go b/crashreport.go index 80344b0..2ad5f62 100644 --- a/crashreport.go +++ b/crashreport.go @@ -1,4 +1,3 @@ - package mcla import ( @@ -10,39 +9,38 @@ import ( var ( ErrUnexpectedIndent = errors.New("Crash report details format incorrect: unexpected indent") - ErrMissingColon = errors.New("Crash report details format incorrect: missing colon") + ErrMissingColon = errors.New("Crash report details format incorrect: missing colon") ) var ( - crashReportHeader = strings.ToUpper("---- Minecraft Crash Report ----") - headThreadKey = strings.ToUpper("Head") - affectedLevelKey = strings.ToUpper("Affected level") - descriptionHeader = strings.ToUpper("Description:") - detailsKeyHeader = strings.ToUpper("Details:") - stacktraceHeader = strings.ToUpper("Stacktrace:") - threadKeyHeader = strings.ToUpper("Thread:") + crashReportHeader = strings.ToUpper("---- Minecraft Crash Report ----") + headThreadKey = strings.ToUpper("Head") + affectedLevelKey = strings.ToUpper("Affected level") + descriptionHeader = strings.ToUpper("Description:") + detailsKeyHeader = strings.ToUpper("Details:") + stacktraceHeader = strings.ToUpper("Stacktrace:") + threadKeyHeader = strings.ToUpper("Thread:") ) - -func hasIndent(line []byte)(bool){ +func hasIndent(line []byte) bool { return bytes.HasPrefix(line, ([]byte)("\t")) || bytes.HasPrefix(line, ([]byte)(" ")) } -func hasDbIndent(line []byte)(bool){ +func hasDbIndent(line []byte) bool { return bytes.HasPrefix(line, ([]byte)("\t\t")) || bytes.HasPrefix(line, ([]byte)(" ")) } // Details: type ReportDetails map[string][]string -func parseReportDetails(sc *lineScanner)(d ReportDetails, err error){ +func parseReportDetails(sc *lineScanner) (d ReportDetails, err error) { if !sc.Scan() { return } return parseReportDetails0(sc) } -func parseReportDetails0(sc *lineScanner)(d ReportDetails, err error){ +func parseReportDetails0(sc *lineScanner) (d ReportDetails, err error) { d = make(ReportDetails) line := sc.Bytes() for { @@ -58,10 +56,10 @@ func parseReportDetails0(sc *lineScanner)(d ReportDetails, err error){ return nil, ErrMissingColon } var ( - key string = (string)(bytes.TrimSpace(line[:i])) + key string = (string)(bytes.TrimSpace(line[:i])) values []string ) - if line = bytes.TrimSpace(line[i + 1:]); len(line) > 0 { + if line = bytes.TrimSpace(line[i+1:]); len(line) > 0 { values = []string{(string)(line)} } for { @@ -82,16 +80,16 @@ func parseReportDetails0(sc *lineScanner)(d ReportDetails, err error){ } } -func (d ReportDetails)set(key string, values []string){ +func (d ReportDetails) set(key string, values []string) { d[strings.ToUpper(key)] = values } -func (d ReportDetails)Has(key string)(ok bool){ +func (d ReportDetails) Has(key string) (ok bool) { _, ok = d[strings.ToUpper(key)] return } -func (d ReportDetails)Get(key string)(value string){ +func (d ReportDetails) Get(key string) (value string) { values, ok := d[strings.ToUpper(key)] if !ok { return "" @@ -99,18 +97,17 @@ func (d ReportDetails)Get(key string)(value string){ return strings.Join(values, "\n") } -func (d ReportDetails)GetValues(key string)(values []string){ +func (d ReportDetails) GetValues(key string) (values []string) { return d[strings.ToUpper(key)] } - // -- Head -- type HeadThread struct { Thread string `json:"thread"` Stacktrace Stacktrace `json:"stacktrace"` } -func parseHeadThread(sc *lineScanner)(res HeadThread, err error){ +func parseHeadThread(sc *lineScanner) (res HeadThread, err error) { if !sc.Scan() { return } @@ -142,7 +139,7 @@ type AffectedLevel struct { Stacktrace Stacktrace `json:"stacktrace"` } -func parseAffectedLevel(sc *lineScanner)(res AffectedLevel, err error){ +func parseAffectedLevel(sc *lineScanner) (res AffectedLevel, err error) { if !sc.Scan() { return } @@ -176,7 +173,7 @@ type DetailsItem struct { Details ReportDetails `json:"details"` } -func parseDetailsItem(sc *lineScanner)(res DetailsItem, err error){ +func parseDetailsItem(sc *lineScanner) (res DetailsItem, err error) { if !sc.Scan() { return } @@ -204,15 +201,15 @@ func parseDetailsItem(sc *lineScanner)(res DetailsItem, err error){ return } -type CrashReport struct { // ---- Minecraft Crash Report ---- - Description string `json:"description"` // Description: - Error *JavaError `json:"error"` - HeadThread HeadThread `json:"head"` // -- Head -- - AffectedLevel AffectedLevel `json:"affectedLevel"` // -- Affected level -- - OtherDetails map[string]DetailsItem `json:"others"` // -- -- +type CrashReport struct { // ---- Minecraft Crash Report ---- + Description string `json:"description"` // Description: + Error *JavaError `json:"error"` + HeadThread HeadThread `json:"head"` // -- Head -- + AffectedLevel AffectedLevel `json:"affectedLevel"` // -- Affected level -- + OtherDetails map[string]DetailsItem `json:"others"` // -- -- } -func ParseCrashReport(r io.Reader)(report *CrashReport, err error){ +func ParseCrashReport(r io.Reader) (report *CrashReport, err error) { sc := newLineScanner(r) for { if !sc.Scan() { @@ -250,7 +247,7 @@ func ParseCrashReport(r io.Reader)(report *CrashReport, err error){ } flag = 1 case strings.HasPrefix(uline, "-- ") && strings.HasSuffix(uline, " --"): - name := strings.ToUpper((string)(uline[len("-- "):len(uline) - len(" --")])) + name := strings.ToUpper((string)(uline[len("-- ") : len(uline)-len(" --")])) switch { case name == headThreadKey: if report.HeadThread, err = parseHeadThread(sc); err != nil { @@ -277,6 +274,6 @@ func ParseCrashReport(r io.Reader)(report *CrashReport, err error){ return } -func (report *CrashReport)GetDetails(key string)(value DetailsItem){ +func (report *CrashReport) GetDetails(key string) (value DetailsItem) { return report.OtherDetails[strings.ToUpper(key)] } diff --git a/error_database.go b/error_database.go index 0daa4cc..35e8cae 100644 --- a/error_database.go +++ b/error_database.go @@ -1,4 +1,3 @@ - package mcla type ErrorDesc struct { @@ -14,6 +13,6 @@ type SolutionDesc struct { } type ErrorDB interface { - ForEachErrors(callback func(*ErrorDesc)(error))(err error) - GetSolution(id int)(sol *SolutionDesc, err error) + ForEachErrors(callback func(*ErrorDesc) error) (err error) + GetSolution(id int) (sol *SolutionDesc, err error) } diff --git a/example/just_load_it/index.html b/example/just_load_it/index.html index fb150d4..2f2d5e8 100644 --- a/example/just_load_it/index.html +++ b/example/just_load_it/index.html @@ -7,7 +7,7 @@

MCLA wasm test

- + \ No newline at end of file diff --git a/example/just_load_it/index.js b/example/just_load_it/index.js index a66a592..6e475b3 100644 --- a/example/just_load_it/index.js +++ b/example/just_load_it/index.js @@ -9,7 +9,7 @@ function sleep(ms){ (async function(){ const go = new Go() - const MCLA_WASM_URL = 'https://kmcsr.github.io/mcla/mcla.wasm' + const MCLA_WASM_URL = 'https://globemc.github.io/mcla/mcla.wasm' const res = await WebAssembly.instantiateStreaming(fetch(MCLA_WASM_URL), go.importObject) go.run(res.instance) diff --git a/ghdb/ghdb.go b/ghdb/ghdb.go new file mode 100644 index 0000000..90df9f8 --- /dev/null +++ b/ghdb/ghdb.go @@ -0,0 +1,153 @@ +// Github database +package ghdb + +import ( + "encoding/json" + "fmt" + "io" + "path" + // "sync" + "sync/atomic" + "time" + + "github.com/GlobeMC/mcla" +) + +const syntaxVersion = 0 // 0 means dev + +type UnsupportSyntaxErr struct { + Version int +} + +func (e *UnsupportSyntaxErr) Error() string { + return fmt.Sprintf("MCLA-DB syntax version %d is not supported, please update the application", e.Version) +} + +type versionData struct { + Major int `json:"major"` + Minor int `json:"minor"` + Patch int `json:"patch"` + ErrorIncId int `json:"errorIncId"` + SolutionIncId int `json:"solutionIncId"` +} + +type ErrDB struct { + Fetch func(path string) (io.ReadCloser, error) + + checking atomic.Bool + cachedVersion versionData + lastCheck time.Time +} + +var _ mcla.ErrorDB = (*ErrDB)(nil) + +func (db *ErrDB) fetch(subpaths ...string) (io.ReadCloser, error) { + return db.Fetch(path.Join(subpaths...)) +} + +func (db *ErrDB) fetchGhDBVersion() (v versionData, err error) { + var res io.ReadCloser + if res, err = db.fetch("version.json"); err != nil { + return + } + defer res.Close() + if err = json.NewDecoder(res).Decode(&v); err != nil { + return + } + if v.Major != syntaxVersion { + err = &UnsupportSyntaxErr{v.Major} + return + } + return +} + +func (db *ErrDB) checkUpdate() error { + if !db.checking.CompareAndSwap(false, true) { + return nil + } + defer db.checking.Store(false) + + if !db.lastCheck.IsZero() && time.Since(db.lastCheck) <= time.Minute { + return nil + } + + return db.RefreshCache() +} + +func (db *ErrDB) RefreshCache() (err error) { + newVersion, err := db.fetchGhDBVersion() + if err != nil { + return + } + // TODO: do we really need caches? + if newVersion.Major != db.cachedVersion.Major || newVersion.Minor != db.cachedVersion.Minor { + // TODO: refresh all data + db.cachedVersion = newVersion + } else if newVersion.Patch != db.cachedVersion.Patch { + // var wg sync.WaitGroup + // wg.Add(newVersion.ErrorIncId - db.cachedVersion.ErrorIncId) + // for i := db.cachedVersion.ErrorIncId + 1; i <= newVersion.ErrorIncId; i++ { + // go func(i int) { + // defer wg.Done() + // db.GetErrorDesc(i) // refresh cache + // }(i) + // } + // wg.Wait() + db.cachedVersion.ErrorIncId = newVersion.ErrorIncId + // wg.Add(newVersion.SolutionIncId - db.cachedVersion.SolutionIncId) + // for i := db.cachedVersion.SolutionIncId + 1; i <= newVersion.SolutionIncId; i++ { + // go func(i int) { + // defer wg.Done() + // db.GetSolution(i) // refresh cache + // }(i) + // } + // wg.Wait() + db.cachedVersion.SolutionIncId = newVersion.SolutionIncId + db.cachedVersion.Patch = newVersion.Patch + } + + db.lastCheck = time.Now() + return +} + +func (db *ErrDB) GetErrorDesc(id int) (desc *mcla.ErrorDesc, err error) { + res, err := db.fetch("errors", fmt.Sprintf("%d.json", id)) + if err != nil { + return + } + defer res.Close() + desc = new(mcla.ErrorDesc) + if err = json.NewDecoder(res).Decode(&desc); err != nil { + return + } + return +} + +func (db *ErrDB) ForEachErrors(callback func(*mcla.ErrorDesc) error) (err error) { + db.checkUpdate() + + for i := 1; i <= db.cachedVersion.ErrorIncId; i++ { + var desc *mcla.ErrorDesc + if desc, err = db.GetErrorDesc(i); err != nil { + return + } + if err = callback(desc); err != nil { + return + } + } + return +} + +func (db *ErrDB) GetSolution(id int) (sol *mcla.SolutionDesc, err error) { + var res io.ReadCloser + if res, err = db.fetch("solutions", fmt.Sprintf("%d.json", id)); err != nil { + return + } + defer res.Close() + sol = new(mcla.SolutionDesc) + if err = json.NewDecoder(res).Decode(sol); err != nil { + sol = nil + return + } + return +} diff --git a/jerror.go b/jerror.go index 177170c..5522a56 100644 --- a/jerror.go +++ b/jerror.go @@ -1,4 +1,3 @@ - package mcla import ( @@ -20,20 +19,20 @@ type ( CausedBy *JavaError `json:"causedBy"` // extra infos - LineNo int `json:"lineNo"` // which line did the error start + LineNo int `json:"lineNo"` // which line did the error start } StackInfo struct { - Raw string `json:"raw"` - Class string `json:"class"` - Method string `json:"method"` + Raw string `json:"raw"` + Class string `json:"class"` + Method string `json:"method"` } // Stacktrace: Stacktrace []StackInfo ) -func parseStackInfoFrom(line string)(s StackInfo, ok bool){ +func parseStackInfoFrom(line string) (s StackInfo, ok bool) { res := stackInfoMatcher.FindStringSubmatch(line) if res == nil { return @@ -45,17 +44,17 @@ func parseStackInfoFrom(line string)(s StackInfo, ok bool){ return } -func parseStacktrace(sc *lineScanner)(st Stacktrace){ +func parseStacktrace(sc *lineScanner) (st Stacktrace) { if !sc.Scan() { return } return parseStacktrace0(sc) } -func parseStacktrace0(sc *lineScanner)(st Stacktrace){ +func parseStacktrace0(sc *lineScanner) (st Stacktrace) { var ( info StackInfo - ok bool + ok bool ) for { line := sc.Text() @@ -70,21 +69,20 @@ func parseStacktrace0(sc *lineScanner)(st Stacktrace){ return } - -func parseJavaError(sc *lineScanner)(je *JavaError){ +func parseJavaError(sc *lineScanner) (je *JavaError) { if !sc.Scan() { return } return parseJavaError0(sc.Text(), sc) } -func parseJavaError0(line string, sc *lineScanner)(je *JavaError){ +func parseJavaError0(line string, sc *lineScanner) (je *JavaError) { je = new(JavaError) i := strings.IndexByte(line, ':') if i == -1 { je.Message = line - }else{ - je.Class, je.Message = line[:i], strings.TrimSpace(line[i + 1:]) + } else { + je.Class, je.Message = line[:i], strings.TrimSpace(line[i+1:]) } je.Stacktrace = parseStacktrace(sc) line = sc.Text() @@ -94,13 +92,13 @@ func parseJavaError0(line string, sc *lineScanner)(je *JavaError){ return } -func scanJavaErrors(r io.Reader, cb func(*JavaError))(err error){ +func scanJavaErrors(r io.Reader, cb func(*JavaError)) (err error) { sc := newLineScanner(r) if !sc.Scan() { return sc.Err() } var ( - line string + line string lineNo int ) for { @@ -121,7 +119,7 @@ func scanJavaErrors(r io.Reader, cb func(*JavaError))(err error){ if em := javaErrorMatcher.FindStringSubmatch(l2); em != nil { line = l2 emsg = em - }else{ + } else { emsg[2] += "\n" + l2 } if !sc.Scan() { @@ -129,12 +127,12 @@ func scanJavaErrors(r io.Reader, cb func(*JavaError))(err error){ } } st := parseStacktrace0(sc) - if st != nil { // if stacktrace exists + if st != nil { // if stacktrace exists je := &JavaError{ - Class: emsg[1], - Message: emsg[2], + Class: emsg[1], + Message: emsg[2], Stacktrace: st, - LineNo: lineNo, + LineNo: lineNo, } if line, ok := strings.CutPrefix(sc.Text(), "Caused by: "); ok { je.CausedBy = parseJavaError0(line, sc) @@ -144,20 +142,20 @@ func scanJavaErrors(r io.Reader, cb func(*JavaError))(err error){ } } -func ScanJavaErrors(r io.Reader)(res []*JavaError, err error){ +func ScanJavaErrors(r io.Reader) (res []*JavaError, err error) { res = make([]*JavaError, 0, 3) - err = scanJavaErrors(r, func(je *JavaError){ + err = scanJavaErrors(r, func(je *JavaError) { res = append(res, je) }) return } -func ScanJavaErrorsIntoChan(r io.Reader)(<-chan *JavaError, <-chan error){ +func ScanJavaErrorsIntoChan(r io.Reader) (<-chan *JavaError, <-chan error) { resCh := make(chan *JavaError, 3) errCh := make(chan error, 0) - go func(){ + go func() { defer close(resCh) - err := scanJavaErrors(r, func(je *JavaError){ + err := scanJavaErrors(r, func(je *JavaError) { resCh <- je }) if err != nil { diff --git a/package.go b/package.go index 1183653..501379e 100644 --- a/package.go +++ b/package.go @@ -1,9 +1,8 @@ - /** * Minecraft Log Analyzer * Copyright (C) 2023 Kevin Z * All rights reserved - * + * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or @@ -16,7 +15,6 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ package mcla diff --git a/scanner.go b/scanner.go index 86bef73..7b0aecd 100644 --- a/scanner.go +++ b/scanner.go @@ -1,4 +1,3 @@ - package mcla import ( @@ -11,16 +10,16 @@ type lineScanner struct { *bufio.Scanner } -func newLineScanner(r io.Reader)(*lineScanner){ +func newLineScanner(r io.Reader) *lineScanner { bs := bufio.NewScanner(r) - bs.Buffer(make([]byte, 16 * 1024), 1024 * 1024) // 1MB per line, large enough? + bs.Buffer(make([]byte, 16*1024), 1024*1024) // 1MB per line, large enough? return &lineScanner{ - count: 0, + count: 0, Scanner: bs, } } -func (s *lineScanner)Scan()(bool){ +func (s *lineScanner) Scan() bool { if !s.Scanner.Scan() { return false } @@ -28,6 +27,6 @@ func (s *lineScanner)Scan()(bool){ return true } -func (s *lineScanner)Count()(int){ +func (s *lineScanner) Count() int { return s.count } diff --git a/utils.go b/utils.go index 79cebfd..ca3c59d 100644 --- a/utils.go +++ b/utils.go @@ -1,35 +1,34 @@ - package mcla import ( "strings" ) -func rsplit(line string, b byte)(left, right string){ +func rsplit(line string, b byte) (left, right string) { i := strings.LastIndexByte(line, b) if i < 0 { return "", line } - return line[:i], line[i + 1:] + return line[:i], line[i+1:] } -func split(line string, b byte)(left, right string){ +func split(line string, b byte) (left, right string) { i := strings.IndexByte(line, b) if i < 0 { return line, "" } - return line[:i], line[i + 1:] + return line[:i], line[i+1:] } -func lcsSplit[T comparable](a, b []T)(n int, a1, a2, b1, b2 []T){ +func lcsSplit[T comparable](a, b []T) (n int, a1, a2, b1, b2 []T) { if len(a) == 0 || len(b) == 0 { return 0, a, nil, b, nil } type ele struct { a, b int - len int + len int } - ch := make([]ele, len(b) + 1) + ch := make([]ele, len(b)+1) for i, p := range a { var last ele for j, q := range b { @@ -37,12 +36,12 @@ func lcsSplit[T comparable](a, b []T)(n int, a1, a2, b1, b2 []T){ if p == q { if last.len == 0 { last = ele{i, j, 1} - }else if last.a + last.len == i && last.b + last.len == j { + } else if last.a+last.len == i && last.b+last.len == j { last.len++ } - ch[j + 1] = last - }else if prev := ch[j]; prev.len > cur.len { - ch[j + 1] = prev + ch[j+1] = last + } else if prev := ch[j]; prev.len > cur.len { + ch[j+1] = prev } last = cur } @@ -51,10 +50,10 @@ func lcsSplit[T comparable](a, b []T)(n int, a1, a2, b1, b2 []T){ if res.len == 0 { return } - return res.len, a[:res.a], a[res.a + res.len:], b[:res.b], b[res.b + res.len:] + return res.len, a[:res.a], a[res.a+res.len:], b[:res.b], b[res.b+res.len:] } -func lcsLength[T comparable](a, b []T)(n int){ +func lcsLength[T comparable](a, b []T) (n int) { if len(a) == 0 || len(b) == 0 { return 0 } @@ -63,7 +62,7 @@ func lcsLength[T comparable](a, b []T)(n int){ return } -func lcsPercent[T comparable](a, b []T)(v float32){ +func lcsPercent[T comparable](a, b []T) (v float32) { if len(b) > len(a) { a, b = b, a } @@ -74,6 +73,6 @@ func lcsPercent[T comparable](a, b []T)(v float32){ return n / (float32)(len(a)) } -func lineMatchPercent(a, b string)(float32){ +func lineMatchPercent(a, b string) float32 { return lcsPercent(([]rune)(a), ([]rune)(b)) }