aboutsummaryrefslogtreecommitdiff
path: root/Northstar.Client/mod/scripts/vscripts/ui/menu_ns_moddownload.nut
blob: 3b81c7f9a8a9984e9c6cd1df29119e1d5ce5f5ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
global function DownloadMod
global function DisplayModDownloadErrorDialog
global function FetchVerifiedModsManifesto

global enum eModInstallStatus
{
    MANIFESTO_FETCHING,
    DOWNLOADING,
    CHECKSUMING,
    EXTRACTING,
    DONE,
    FAILED,
    FAILED_READING_ARCHIVE,
    FAILED_WRITING_TO_DISK,
    MOD_FETCHING_FAILED,
    MOD_CORRUPTED,
    NO_DISK_SPACE_AVAILABLE,
    NOT_FOUND
}

const int MB = 1024*1000;


void function FetchVerifiedModsManifesto()
{
	print("Start fetching verified mods manifesto from the Internet")

	// Fetching UI
	DialogData dialogData
	dialogData.header = Localize( "#MANIFESTO_FETCHING_TITLE" )
	dialogData.message = Localize( "#MANIFESTO_FETCHING_TEXT" )
	dialogData.showSpinner = true;

	// Prevent user from closing dialog
	dialogData.forceChoice = true;
	OpenDialog( dialogData )

	// Do the actual fetching
	NSFetchVerifiedModsManifesto()

	ModInstallState state = NSGetModInstallState()
	while ( state.status == eModInstallStatus.MANIFESTO_FETCHING )
	{
		state = NSGetModInstallState()
		WaitFrame()
	}

	// Close dialog when manifesto has been received
	CloseActiveMenu()
}

bool function DownloadMod( RequiredModInfo mod, string serverName )
{
	// Downloading mod UI
	DialogData dialogData
	dialogData.header = Localize( "#DOWNLOADING_MOD_TITLE" )
	dialogData.message = Localize( "#DOWNLOADING_MOD_TEXT", mod.name, mod.version )
	dialogData.showSpinner = true;

	// Prevent user from closing dialog
	dialogData.forceChoice = true;
	OpenDialog( dialogData )

	// Save reference to UI elements, to update their content
	var menu = GetMenu( "Dialog" )
	var header = Hud_GetChild( menu, "DialogHeader" )
	var body = GetSingleElementByClassname( menu, "DialogMessageClass" )

	// Start actual mod downloading
	NSDownloadMod( mod.name, mod.version )

	ModInstallState state = NSGetModInstallState()
	while ( state.status < eModInstallStatus.DONE )
	{
		state = NSGetModInstallState()
		UpdateModDownloadDialog( mod, state, menu, header, body )
		WaitFrame()
	}

	printt( "Mod status:", state.status )

	// Close loading dialog
	CloseActiveMenu()

	return state.status == eModInstallStatus.DONE
}

void function UpdateModDownloadDialog( RequiredModInfo mod, ModInstallState state, var menu, var header, var body )
{
	switch ( state.status )
	{
	case eModInstallStatus.MANIFESTO_FETCHING:
		Hud_SetText( header, Localize( "#MANIFESTO_FETCHING_TITLE" ) )
		Hud_SetText( body, Localize( "#MANIFESTO_FETCHING_TEXT" ) )
		break
	case eModInstallStatus.DOWNLOADING:
		Hud_SetText( header, Localize( "#DOWNLOADING_MOD_TITLE_W_PROGRESS", string( state.ratio ) ) )
		Hud_SetText( body, Localize( "#DOWNLOADING_MOD_TEXT_W_PROGRESS", mod.name, mod.version, floor( state.progress / MB ), floor( state.total / MB ) ) )
		break
	case eModInstallStatus.CHECKSUMING:
		Hud_SetText( header, Localize( "#CHECKSUMING_TITLE" ) )
		Hud_SetText( body, Localize( "#CHECKSUMING_TEXT", mod.name, mod.version ) )
		break
	case eModInstallStatus.EXTRACTING:
		Hud_SetText( header, Localize( "#EXTRACTING_MOD_TITLE", string( state.ratio ) ) )
		Hud_SetText( body, Localize( "#EXTRACTING_MOD_TEXT", mod.name, mod.version, floor( state.progress / MB ), floor( state.total / MB ) ) )
		break
	default:
		break
	}
}

void function DisplayModDownloadErrorDialog( string modName )
{
	ModInstallState state = NSGetModInstallState()

	DialogData dialogData
	dialogData.header = Localize( "#FAILED_DOWNLOADING", modName )
	dialogData.image = $"ui/menu/common/dialog_error"

	switch ( state.status )
	{
    case eModInstallStatus.FAILED_READING_ARCHIVE:
		dialogData.message = Localize( "#FAILED_READING_ARCHIVE" )
		break
    case eModInstallStatus.FAILED_WRITING_TO_DISK:
		dialogData.message = Localize( "#FAILED_WRITING_TO_DISK" )
		break
    case eModInstallStatus.MOD_FETCHING_FAILED:
		dialogData.message = Localize( "#MOD_FETCHING_FAILED" )
		break
    case eModInstallStatus.MOD_CORRUPTED:
		dialogData.message = Localize( "#MOD_CORRUPTED" )
		break
    case eModInstallStatus.NO_DISK_SPACE_AVAILABLE:
		dialogData.message = Localize( "#NO_DISK_SPACE_AVAILABLE" )
		break
    case eModInstallStatus.NOT_FOUND:
		dialogData.message = Localize( "#NOT_FOUND" )
		break
	case eModInstallStatus.FAILED:
	default:
		dialogData.message = Localize( "#MOD_FETCHING_FAILED_GENERAL" )
		break
	}

	AddDialogButton( dialogData, "#DISMISS" )
	AddDialogFooter( dialogData, "#A_BUTTON_SELECT" )
	AddDialogFooter( dialogData, "#B_BUTTON_DISMISS_RUI" )

	OpenDialog( dialogData )
}