2020-06-30 12:36:48 -04:00
|
|
|
// Package d2app contains the OpenDiablo2 application shell
|
|
|
|
package d2app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-07-01 00:10:15 -04:00
|
|
|
"container/ring"
|
2020-06-30 12:36:48 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/gif"
|
|
|
|
"image/png"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
"github.com/pkg/profile"
|
|
|
|
"golang.org/x/image/colornames"
|
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
|
|
|
|
2020-09-14 14:47:11 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2tbl"
|
2020-06-30 12:36:48 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-08-05 22:31:56 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
|
2020-06-30 12:36:48 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-09-14 14:47:11 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2020-06-30 12:36:48 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2gui"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2screen"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2game/d2gamescreen"
|
2020-07-14 13:11:23 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2networking/d2client/d2clientconnectiontype"
|
2020-06-30 12:36:48 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2script"
|
|
|
|
)
|
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
// these are used for debug print info
|
|
|
|
const (
|
|
|
|
fpsX, fpsY = 5, 565
|
|
|
|
memInfoX, memInfoY = 670, 5
|
|
|
|
debugLineHeight = 16
|
|
|
|
errMsgPadding = 20
|
|
|
|
)
|
|
|
|
|
2020-06-30 12:36:48 -04:00
|
|
|
// App represents the main application for the engine
|
|
|
|
type App struct {
|
|
|
|
lastTime float64
|
|
|
|
lastScreenAdvance float64
|
|
|
|
showFPS bool
|
|
|
|
timeScale float64
|
|
|
|
captureState captureState
|
|
|
|
capturePath string
|
|
|
|
captureFrames []*image.RGBA
|
|
|
|
gitBranch string
|
|
|
|
gitCommit string
|
2020-09-12 16:51:30 -04:00
|
|
|
asset *d2asset.AssetManager
|
2020-07-13 20:29:17 -04:00
|
|
|
inputManager d2interface.InputManager
|
2020-06-30 12:36:48 -04:00
|
|
|
terminal d2interface.Terminal
|
2020-07-11 11:24:04 -04:00
|
|
|
scriptEngine *d2script.ScriptEngine
|
2020-06-30 12:36:48 -04:00
|
|
|
audio d2interface.AudioProvider
|
2020-07-03 14:00:56 -04:00
|
|
|
renderer d2interface.Renderer
|
2020-08-05 22:31:56 -04:00
|
|
|
screen *d2screen.ScreenManager
|
2020-08-06 10:30:23 -04:00
|
|
|
ui *d2ui.UIManager
|
2020-07-01 00:10:15 -04:00
|
|
|
tAllocSamples *ring.Ring
|
2020-09-18 16:10:52 -04:00
|
|
|
guiManager *d2gui.GuiManager
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type bindTerminalEntry struct {
|
|
|
|
name string
|
|
|
|
description string
|
|
|
|
action interface{}
|
|
|
|
}
|
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
const (
|
|
|
|
bytesToMegabyte = 1024 * 1024
|
|
|
|
nSamplesTAlloc = 100
|
|
|
|
debugPopN = 6
|
|
|
|
)
|
2020-06-30 12:36:48 -04:00
|
|
|
|
|
|
|
// Create creates a new instance of the application
|
2020-07-03 14:00:56 -04:00
|
|
|
func Create(gitBranch, gitCommit string,
|
2020-07-13 20:29:17 -04:00
|
|
|
inputManager d2interface.InputManager,
|
2020-07-03 14:00:56 -04:00
|
|
|
terminal d2interface.Terminal,
|
2020-07-11 11:24:04 -04:00
|
|
|
scriptEngine *d2script.ScriptEngine,
|
2020-07-03 14:00:56 -04:00
|
|
|
audio d2interface.AudioProvider,
|
2020-08-06 10:30:23 -04:00
|
|
|
renderer d2interface.Renderer,
|
2020-09-12 16:51:30 -04:00
|
|
|
asset *d2asset.AssetManager,
|
2020-08-06 10:30:23 -04:00
|
|
|
) *App {
|
2020-09-12 16:51:30 -04:00
|
|
|
uiManager := d2ui.NewUIManager(asset, renderer, inputManager, audio)
|
2020-08-06 10:30:23 -04:00
|
|
|
|
2020-06-30 12:36:48 -04:00
|
|
|
result := &App{
|
2020-07-01 19:24:09 -04:00
|
|
|
gitBranch: gitBranch,
|
|
|
|
gitCommit: gitCommit,
|
2020-07-13 20:29:17 -04:00
|
|
|
inputManager: inputManager,
|
2020-07-01 19:24:09 -04:00
|
|
|
terminal: terminal,
|
2020-07-11 11:24:04 -04:00
|
|
|
scriptEngine: scriptEngine,
|
2020-07-01 19:24:09 -04:00
|
|
|
audio: audio,
|
2020-07-03 14:00:56 -04:00
|
|
|
renderer: renderer,
|
2020-08-06 10:30:23 -04:00
|
|
|
ui: uiManager,
|
2020-09-12 16:51:30 -04:00
|
|
|
asset: asset,
|
2020-07-01 00:10:15 -04:00
|
|
|
tAllocSamples: createZeroedRing(nSamplesTAlloc),
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-27 15:00:50 -04:00
|
|
|
if result.gitBranch == "" {
|
|
|
|
result.gitBranch = "Local Build"
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:36:48 -04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the application and kicks off the entire game process
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) Run() error {
|
2020-07-01 19:24:09 -04:00
|
|
|
profileOption := kingpin.Flag("profile", "Profiles the program, one of (cpu, mem, block, goroutine, trace, thread, mutex)").String()
|
|
|
|
kingpin.Parse()
|
2020-06-30 12:36:48 -04:00
|
|
|
|
2020-07-01 19:24:09 -04:00
|
|
|
if len(*profileOption) > 0 {
|
|
|
|
profiler := enableProfiler(*profileOption)
|
|
|
|
if profiler != nil {
|
|
|
|
defer profiler.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
windowTitle := fmt.Sprintf("OpenDiablo2 (%s)", a.gitBranch)
|
2020-06-30 12:36:48 -04:00
|
|
|
// If we fail to initialize, we will show the error screen
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := a.initialize(); err != nil {
|
|
|
|
if gameErr := a.renderer.Run(updateInitError, 800, 600, windowTitle); gameErr != nil {
|
2020-07-08 09:16:16 -04:00
|
|
|
return gameErr
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-08 09:16:16 -04:00
|
|
|
return err
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
a.ToMainMenu()
|
2020-06-30 12:36:48 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := a.renderer.Run(a.update, 800, 600, windowTitle); err != nil {
|
2020-07-08 09:16:16 -04:00
|
|
|
return err
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
2020-07-08 09:16:16 -04:00
|
|
|
|
|
|
|
return nil
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) initialize() error {
|
|
|
|
a.timeScale = 1.0
|
2020-09-08 15:58:35 -04:00
|
|
|
a.lastTime = d2util.Now()
|
2020-07-23 12:56:50 -04:00
|
|
|
a.lastScreenAdvance = a.lastTime
|
2020-06-30 12:36:48 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
a.renderer.SetWindowIcon("d2logo.png")
|
|
|
|
a.terminal.BindLogger()
|
2020-06-30 12:36:48 -04:00
|
|
|
|
|
|
|
terminalActions := [...]bindTerminalEntry{
|
2020-07-23 12:56:50 -04:00
|
|
|
{"dumpheap", "dumps the heap to pprof/heap.pprof", a.dumpHeap},
|
|
|
|
{"fullscreen", "toggles fullscreen", a.toggleFullScreen},
|
2020-08-04 11:16:06 -04:00
|
|
|
{"capframe", "captures a still frame", a.setupCaptureFrame},
|
2020-07-23 12:56:50 -04:00
|
|
|
{"capgifstart", "captures an animation (start)", a.startAnimationCapture},
|
|
|
|
{"capgifstop", "captures an animation (stop)", a.stopAnimationCapture},
|
|
|
|
{"vsync", "toggles vsync", a.toggleVsync},
|
|
|
|
{"fps", "toggle fps counter", a.toggleFpsCounter},
|
|
|
|
{"timescale", "set scalar for elapsed time", a.setTimeScale},
|
|
|
|
{"quit", "exits the game", a.quitGame},
|
|
|
|
{"screen-gui", "enters the gui playground screen", a.enterGuiPlayground},
|
|
|
|
{"js", "eval JS scripts", a.evalJS},
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx := range terminalActions {
|
|
|
|
action := &terminalActions[idx]
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := a.terminal.BindAction(action.name, action.description, action.action); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:10:52 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
a.guiManager, err = d2gui.CreateGuiManager(a.asset, a.inputManager)
|
|
|
|
if err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:10:52 -04:00
|
|
|
a.screen = d2screen.NewScreenManager(a.ui, a.guiManager)
|
|
|
|
|
2020-07-22 15:03:03 -04:00
|
|
|
config := d2config.Config
|
2020-07-23 12:56:50 -04:00
|
|
|
a.audio.SetVolumes(config.BgmVolume, config.SfxVolume)
|
2020-06-30 12:36:48 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := a.loadStrings(); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
a.ui.Initialize()
|
2020-06-30 12:36:48 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) loadStrings() error {
|
2020-06-30 12:36:48 -04:00
|
|
|
tablePaths := []string{
|
|
|
|
d2resource.PatchStringTable,
|
|
|
|
d2resource.ExpansionStringTable,
|
|
|
|
d2resource.StringTable,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tablePath := range tablePaths {
|
2020-09-12 16:51:30 -04:00
|
|
|
data, err := a.asset.LoadFile(tablePath)
|
2020-06-30 12:36:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-08 15:58:35 -04:00
|
|
|
d2tbl.LoadTextDictionary(data)
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) renderDebug(target d2interface.Surface) error {
|
|
|
|
if !a.showFPS {
|
2020-06-30 12:36:48 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
vsyncEnabled := a.renderer.GetVSyncEnabled()
|
|
|
|
fps := a.renderer.CurrentFPS()
|
|
|
|
cx, cy := a.renderer.GetCursorPos()
|
2020-06-30 12:36:48 -04:00
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
target.PushTranslation(fpsX, fpsY)
|
2020-07-17 18:51:44 -04:00
|
|
|
target.DrawTextf("vsync:" + strconv.FormatBool(vsyncEnabled) + "\nFPS:" + strconv.Itoa(int(fps)))
|
2020-06-30 12:36:48 -04:00
|
|
|
target.Pop()
|
|
|
|
|
|
|
|
var m runtime.MemStats
|
|
|
|
|
|
|
|
runtime.ReadMemStats(&m)
|
2020-08-04 11:16:06 -04:00
|
|
|
target.PushTranslation(memInfoX, memInfoY)
|
2020-07-17 18:51:44 -04:00
|
|
|
target.DrawTextf("Alloc " + strconv.FormatInt(int64(m.Alloc)/bytesToMegabyte, 10))
|
2020-08-04 11:16:06 -04:00
|
|
|
target.PushTranslation(0, debugLineHeight)
|
2020-07-23 12:56:50 -04:00
|
|
|
target.DrawTextf("TAlloc/s " + strconv.FormatFloat(a.allocRate(m.TotalAlloc, fps), 'f', 2, 64))
|
2020-08-04 11:16:06 -04:00
|
|
|
target.PushTranslation(0, debugLineHeight)
|
2020-07-17 18:51:44 -04:00
|
|
|
target.DrawTextf("Pause " + strconv.FormatInt(int64(m.PauseTotalNs/bytesToMegabyte), 10))
|
2020-08-04 11:16:06 -04:00
|
|
|
target.PushTranslation(0, debugLineHeight)
|
2020-07-17 18:51:44 -04:00
|
|
|
target.DrawTextf("HeapSys " + strconv.FormatInt(int64(m.HeapSys/bytesToMegabyte), 10))
|
2020-08-04 11:16:06 -04:00
|
|
|
target.PushTranslation(0, debugLineHeight)
|
2020-07-17 18:51:44 -04:00
|
|
|
target.DrawTextf("NumGC " + strconv.FormatInt(int64(m.NumGC), 10))
|
2020-08-04 11:16:06 -04:00
|
|
|
target.PushTranslation(0, debugLineHeight)
|
2020-07-17 18:51:44 -04:00
|
|
|
target.DrawTextf("Coords " + strconv.FormatInt(int64(cx), 10) + "," + strconv.FormatInt(int64(cy), 10))
|
2020-07-08 09:16:16 -04:00
|
|
|
target.PopN(debugPopN)
|
2020-06-30 12:36:48 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) renderCapture(target d2interface.Surface) error {
|
2020-06-30 12:36:48 -04:00
|
|
|
cleanupCapture := func() {
|
2020-07-23 12:56:50 -04:00
|
|
|
a.captureState = captureStateNone
|
|
|
|
a.capturePath = ""
|
|
|
|
a.captureFrames = nil
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
switch a.captureState {
|
2020-06-30 12:36:48 -04:00
|
|
|
case captureStateFrame:
|
|
|
|
defer cleanupCapture()
|
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
if err := a.doCaptureFrame(target); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case captureStateGif:
|
2020-08-04 11:16:06 -04:00
|
|
|
a.doCaptureGif(target)
|
2020-06-30 12:36:48 -04:00
|
|
|
case captureStateNone:
|
2020-07-23 12:56:50 -04:00
|
|
|
if len(a.captureFrames) > 0 {
|
2020-06-30 12:36:48 -04:00
|
|
|
defer cleanupCapture()
|
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
if err := a.convertFramesToGif(); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) render(target d2interface.Surface) error {
|
2020-08-05 22:31:56 -04:00
|
|
|
if err := a.screen.Render(target); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
a.ui.Render(target)
|
2020-06-30 12:36:48 -04:00
|
|
|
|
2020-09-18 16:10:52 -04:00
|
|
|
if err := a.guiManager.Render(target); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := a.renderDebug(target); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := a.renderCapture(target); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := a.terminal.Render(target); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:59:17 -04:00
|
|
|
func (a *App) advance(elapsed, elapsedUnscaled, current float64) error {
|
2020-07-23 12:56:50 -04:00
|
|
|
elapsedLastScreenAdvance := (current - a.lastScreenAdvance) * a.timeScale
|
2020-06-30 12:36:48 -04:00
|
|
|
|
2020-07-27 15:00:50 -04:00
|
|
|
a.lastScreenAdvance = current
|
2020-06-30 12:36:48 -04:00
|
|
|
|
2020-08-05 22:31:56 -04:00
|
|
|
if err := a.screen.Advance(elapsedLastScreenAdvance); err != nil {
|
2020-07-27 15:00:50 -04:00
|
|
|
return err
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
a.ui.Advance(elapsed)
|
2020-06-30 12:36:48 -04:00
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := a.inputManager.Advance(elapsed, current); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-18 16:10:52 -04:00
|
|
|
if err := a.guiManager.Advance(elapsed); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-30 12:59:17 -04:00
|
|
|
if err := a.terminal.Advance(elapsedUnscaled); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) update(target d2interface.Surface) error {
|
2020-09-08 15:58:35 -04:00
|
|
|
currentTime := d2util.Now()
|
2020-08-04 11:16:06 -04:00
|
|
|
elapsedTimeUnscaled := currentTime - a.lastTime
|
2020-07-30 12:59:17 -04:00
|
|
|
elapsedTime := elapsedTimeUnscaled * a.timeScale
|
2020-07-23 12:56:50 -04:00
|
|
|
a.lastTime = currentTime
|
2020-06-30 12:36:48 -04:00
|
|
|
|
2020-07-30 12:59:17 -04:00
|
|
|
if err := a.advance(elapsedTime, elapsedTimeUnscaled, currentTime); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
if err := a.render(target); err != nil {
|
2020-06-30 12:36:48 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if target.GetDepth() > 0 {
|
|
|
|
return errors.New("detected surface stack leak")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) allocRate(totalAlloc uint64, fps float64) float64 {
|
|
|
|
a.tAllocSamples.Value = totalAlloc
|
|
|
|
a.tAllocSamples = a.tAllocSamples.Next()
|
|
|
|
deltaAllocPerFrame := float64(totalAlloc-a.tAllocSamples.Value.(uint64)) / nSamplesTAlloc
|
2020-07-01 00:10:15 -04:00
|
|
|
|
|
|
|
return deltaAllocPerFrame * fps / bytesToMegabyte
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) dumpHeap() {
|
2020-09-14 14:49:31 -04:00
|
|
|
if _, err := os.Stat("./pprof/"); os.IsNotExist(err) {
|
|
|
|
if err := os.Mkdir("./pprof/", 0750); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
fileOut, err := os.Create("./pprof/heap.pprof")
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-06-30 12:36:48 -04:00
|
|
|
|
|
|
|
if err := pprof.WriteHeapProfile(fileOut); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fileOut.Close(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) evalJS(code string) {
|
|
|
|
val, err := a.scriptEngine.Eval(code)
|
2020-07-11 11:24:04 -04:00
|
|
|
if err != nil {
|
2020-07-23 12:56:50 -04:00
|
|
|
a.terminal.OutputErrorf("%s", err)
|
2020-07-11 11:24:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-13 20:29:17 -04:00
|
|
|
log.Printf("%s", val)
|
2020-07-11 11:24:04 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) toggleFullScreen() {
|
|
|
|
fullscreen := !a.renderer.IsFullScreen()
|
|
|
|
a.renderer.SetFullScreen(fullscreen)
|
|
|
|
a.terminal.OutputInfof("fullscreen is now: %v", fullscreen)
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
func (a *App) setupCaptureFrame(path string) {
|
2020-07-23 12:56:50 -04:00
|
|
|
a.captureState = captureStateFrame
|
|
|
|
a.capturePath = path
|
|
|
|
a.captureFrames = nil
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
func (a *App) doCaptureFrame(target d2interface.Surface) error {
|
|
|
|
fp, err := os.Create(a.capturePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := fp.Close(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
screenshot := target.Screenshot()
|
|
|
|
if err := png.Encode(fp, screenshot); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("saved frame to %s", a.capturePath)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) doCaptureGif(target d2interface.Surface) {
|
|
|
|
screenshot := target.Screenshot()
|
|
|
|
a.captureFrames = append(a.captureFrames, screenshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) convertFramesToGif() error {
|
|
|
|
fp, err := os.Create(a.capturePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := fp.Close(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var (
|
|
|
|
framesTotal = len(a.captureFrames)
|
|
|
|
framesPal = make([]*image.Paletted, framesTotal)
|
|
|
|
frameDelays = make([]int, framesTotal)
|
|
|
|
framesPerCPU = framesTotal / runtime.NumCPU()
|
|
|
|
)
|
|
|
|
|
|
|
|
var waitGroup sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < framesTotal; i += framesPerCPU {
|
|
|
|
waitGroup.Add(1)
|
|
|
|
|
|
|
|
go func(start, end int) {
|
|
|
|
defer waitGroup.Done()
|
|
|
|
|
|
|
|
for j := start; j < end; j++ {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
if err := gif.Encode(&buffer, a.captureFrames[j], nil); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
framePal, err := gif.Decode(&buffer)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
framesPal[j] = framePal.(*image.Paletted)
|
|
|
|
frameDelays[j] = 5
|
|
|
|
}
|
2020-08-05 00:03:33 -04:00
|
|
|
}(i, d2math.MinInt(i+framesPerCPU, framesTotal))
|
2020-08-04 11:16:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
waitGroup.Wait()
|
|
|
|
|
|
|
|
if err := gif.EncodeAll(fp, &gif.GIF{Image: framesPal, Delay: frameDelays}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("saved animation to %s", a.capturePath)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) startAnimationCapture(path string) {
|
|
|
|
a.captureState = captureStateGif
|
|
|
|
a.capturePath = path
|
|
|
|
a.captureFrames = nil
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) stopAnimationCapture() {
|
|
|
|
a.captureState = captureStateNone
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) toggleVsync() {
|
|
|
|
vsync := !a.renderer.GetVSyncEnabled()
|
|
|
|
a.renderer.SetVSyncEnabled(vsync)
|
|
|
|
a.terminal.OutputInfof("vsync is now: %v", vsync)
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) toggleFpsCounter() {
|
|
|
|
a.showFPS = !a.showFPS
|
|
|
|
a.terminal.OutputInfof("fps counter is now: %v", a.showFPS)
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) setTimeScale(timeScale float64) {
|
2020-06-30 12:36:48 -04:00
|
|
|
if timeScale <= 0 {
|
2020-07-23 12:56:50 -04:00
|
|
|
a.terminal.OutputErrorf("invalid time scale value")
|
2020-06-30 12:36:48 -04:00
|
|
|
} else {
|
2020-07-23 12:56:50 -04:00
|
|
|
a.terminal.OutputInfof("timescale changed from %f to %f", a.timeScale, timeScale)
|
|
|
|
a.timeScale = timeScale
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) quitGame() {
|
2020-06-30 12:36:48 -04:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2020-07-23 12:56:50 -04:00
|
|
|
func (a *App) enterGuiPlayground() {
|
2020-09-18 16:10:52 -04:00
|
|
|
a.screen.SetNextScreen(d2gamescreen.CreateGuiTestMain(a.renderer, a.guiManager, a.asset))
|
2020-06-30 12:36:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-01 00:10:15 -04:00
|
|
|
func createZeroedRing(n int) *ring.Ring {
|
|
|
|
r := ring.New(n)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
r.Value = uint64(0)
|
|
|
|
r = r.Next()
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:36:48 -04:00
|
|
|
func enableProfiler(profileOption string) interface{ Stop() } {
|
|
|
|
var options []func(*profile.Profile)
|
|
|
|
|
|
|
|
switch strings.ToLower(strings.Trim(profileOption, " ")) {
|
|
|
|
case "cpu":
|
|
|
|
log.Printf("CPU profiling is enabled.")
|
|
|
|
|
|
|
|
options = append(options, profile.CPUProfile)
|
|
|
|
case "mem":
|
|
|
|
log.Printf("Memory profiling is enabled.")
|
|
|
|
|
|
|
|
options = append(options, profile.MemProfile)
|
|
|
|
case "block":
|
|
|
|
log.Printf("Block profiling is enabled.")
|
|
|
|
|
|
|
|
options = append(options, profile.BlockProfile)
|
|
|
|
case "goroutine":
|
|
|
|
log.Printf("Goroutine profiling is enabled.")
|
|
|
|
|
|
|
|
options = append(options, profile.GoroutineProfile)
|
|
|
|
case "trace":
|
|
|
|
log.Printf("Trace profiling is enabled.")
|
|
|
|
|
|
|
|
options = append(options, profile.TraceProfile)
|
|
|
|
case "thread":
|
|
|
|
log.Printf("Thread creation profiling is enabled.")
|
|
|
|
|
|
|
|
options = append(options, profile.ThreadcreationProfile)
|
|
|
|
case "mutex":
|
|
|
|
log.Printf("Mutex profiling is enabled.")
|
|
|
|
|
|
|
|
options = append(options, profile.MutexProfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
options = append(options, profile.ProfilePath("./pprof/"))
|
|
|
|
|
|
|
|
if len(options) > 1 {
|
|
|
|
return profile.Start(options...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateInitError(target d2interface.Surface) error {
|
2020-09-23 13:30:54 -04:00
|
|
|
err := target.Clear(colornames.Darkred)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-03 14:00:56 -04:00
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
target.PushTranslation(errMsgPadding, errMsgPadding)
|
2020-07-17 18:51:44 -04:00
|
|
|
target.DrawTextf(`Could not find the MPQ files in the directory:
|
2020-07-08 09:16:16 -04:00
|
|
|
%s\nPlease put the files and re-run the game.`, d2config.Config.MpqPath)
|
2020-06-30 12:36:48 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-14 13:11:23 -04:00
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
// ToMainMenu forces the game to transition to the Main Menu
|
2020-07-14 13:11:23 -04:00
|
|
|
func (a *App) ToMainMenu() {
|
2020-08-04 11:16:06 -04:00
|
|
|
buildInfo := d2gamescreen.BuildInfo{Branch: a.gitBranch, Commit: a.gitCommit}
|
2020-09-12 16:51:30 -04:00
|
|
|
|
2020-09-20 17:52:01 -04:00
|
|
|
mainMenu, err := d2gamescreen.CreateMainMenu(a, a.asset, a.renderer, a.inputManager, a.audio, a.ui, buildInfo)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
2020-09-12 16:51:30 -04:00
|
|
|
|
2020-08-05 22:31:56 -04:00
|
|
|
a.screen.SetNextScreen(mainMenu)
|
2020-07-14 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
// ToSelectHero forces the game to transition to the Select Hero (create character) screen
|
2020-07-14 13:11:23 -04:00
|
|
|
func (a *App) ToSelectHero(connType d2clientconnectiontype.ClientConnectionType, host string) {
|
2020-09-20 17:52:01 -04:00
|
|
|
selectHero, err := d2gamescreen.CreateSelectHeroClass(a, a.asset, a.renderer, a.audio, a.ui, connType, host)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-05 22:31:56 -04:00
|
|
|
a.screen.SetNextScreen(selectHero)
|
2020-07-14 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
// ToCreateGame forces the game to transition to the Create Game screen
|
2020-07-14 13:11:23 -04:00
|
|
|
func (a *App) ToCreateGame(filePath string, connType d2clientconnectiontype.ClientConnectionType, host string) {
|
2020-09-23 13:30:54 -04:00
|
|
|
gameClient, err := d2client.Create(connType, a.asset, a.scriptEngine)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-07-14 13:11:23 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
if err = gameClient.Open(host, filePath); err != nil {
|
2020-07-14 13:11:23 -04:00
|
|
|
// TODO an error screen should be shown in this case
|
|
|
|
fmt.Printf("can not connect to the host: %s", host)
|
|
|
|
}
|
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
a.screen.SetNextScreen(d2gamescreen.CreateGame(a, a.asset, a.ui, a.renderer, a.inputManager,
|
2020-09-18 16:10:52 -04:00
|
|
|
a.audio, gameClient, a.terminal, a.guiManager))
|
2020-07-14 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
// ToCharacterSelect forces the game to transition to the Character Select (load character) screen
|
2020-07-14 13:11:23 -04:00
|
|
|
func (a *App) ToCharacterSelect(connType d2clientconnectiontype.ClientConnectionType, connHost string) {
|
2020-09-12 16:51:30 -04:00
|
|
|
characterSelect := d2gamescreen.CreateCharacterSelect(a, a.asset, a.renderer, a.inputManager,
|
|
|
|
a.audio, a.ui, connType, connHost)
|
|
|
|
|
2020-08-05 22:31:56 -04:00
|
|
|
a.screen.SetNextScreen(characterSelect)
|
2020-07-14 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
// ToMapEngineTest forces the game to transition to the map engine test screen
|
|
|
|
func (a *App) ToMapEngineTest(region, level int) {
|
2020-09-20 17:52:01 -04:00
|
|
|
met, err := d2gamescreen.CreateMapEngineTest(region, level, a.asset, a.terminal, a.renderer, a.inputManager, a.audio, a.screen)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2020-10-22 01:12:06 -04:00
|
|
|
return
|
2020-09-20 17:52:01 -04:00
|
|
|
}
|
|
|
|
|
2020-08-05 22:31:56 -04:00
|
|
|
a.screen.SetNextScreen(met)
|
2020-07-14 13:11:23 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:16:06 -04:00
|
|
|
// ToCredits forces the game to transition to the credits screen
|
2020-07-14 13:11:23 -04:00
|
|
|
func (a *App) ToCredits() {
|
2020-09-12 16:51:30 -04:00
|
|
|
a.screen.SetNextScreen(d2gamescreen.CreateCredits(a, a.asset, a.renderer, a.ui))
|
2020-07-14 13:11:23 -04:00
|
|
|
}
|