summaryrefslogtreecommitdiffhomepage
path: root/dragon/itch_wizard.rb
blob: d87d07cb176e04876cfa737587c8d94e351937a3 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Copyright 2019 DragonRuby LLC
# MIT License
# itch_wizard.rb has been released under MIT (*only this file*).

class ItchWizard
  def steps
    [
      :check_metadata,
      :deploy
    ]
  end

  def metadata_file_path
    "metadata/game_metadata.txt"
  end

  def get_metadata
    metadata = $gtk.read_file metadata_file_path

    if !metadata
      write_blank_metadata
      metadata = $gtk.read_file metadata_file_path
    end

    dev_id, dev_title, game_id, game_title, version, icon = *metadata.each_line.to_a

    {
      dev_id: dev_id.strip,
      dev_title: dev_title.strip,
      game_id: game_id.strip,
      game_title: game_title.strip,
      version: version.strip,
      icon: icon.strip
    }
  end

  def write_blank_metadata
      $gtk.write_file metadata_file_path, <<-S.strip
#devid=myname
#devtitle=My Name
#gameid=mygame
#gametitle=My Game
#version=0.1
#icon=metadata/icon.png
S
  end

  def check_metadata
    metadata_text = $gtk.read_file metadata_file_path
    if !metadata_text
      write_blank_metadata
    end

    if metadata_text.each_line.to_a.length != 6
      write_blank_metadata
    end

    log "* INFO: Contents of #{metadata_file_path}:"
    log "#+begin_src txt"
    metadata_text.each_line { |l| log "  #{l}" }
    log "#+end_src"
    metadata = get_metadata

    if metadata[:dev_id].start_with?("#") || !@dev_id
      log "* PROMPT: Please provide your username for Itch."
      $console.set_command "$wizards.itch.set_dev_id \"your-itch-username\""
      return :need_dev_id
    end

    if metadata[:dev_title].start_with?("#") || !@dev_title
      log "* PROMPT: Please provide developer's/company's name that you want displayed."
      $console.set_command "$wizards.itch.set_dev_title \"Your Name\""
      return :need_dev_title
    end

    if metadata[:game_id].start_with?("#") || !@game_id
      log "* PROMPT: Please provide the id for you game. This is the id you specified when you set up a new game page on Itch."
      $console.set_command "$wizards.itch.set_game_id \"your-game-id\""
      return :need_game_id
    end

    if metadata[:game_title].start_with?("#") || !@game_title
      log "* PROMPT: Please provide the display name for your game. (This can include spaces)"
      $console.set_command "$wizards.itch.set_game_title \"Your Game\""
      return :need_game_title
    end

    if metadata[:version].start_with?("#") || !@version
      log "* PROMPT: Please provide the version for your game."
      $console.set_command "$wizards.itch.set_version \"1.0\""
      return :need_version
    end

    if metadata[:icon].start_with?("#") || !@icon
      log "* PROMPT: Please provide icon path for your game."
      $console.set_command "$wizards.itch.set_icon \"icon.png\""
      return :need_icon
    end

    return :success
  end

  def set_dev_id value
    @dev_id = value
    write_metadata
    start
  end

  def set_dev_title value
    @dev_title = value
    write_metadata
    start
  end

  def set_game_id value
    @game_id = value
    write_metadata
    start
  end

  def set_game_title value
    @game_title = value
    write_metadata
    start
  end

  def set_version value
    @version = value
    write_metadata
    start
  end

  def set_icon value
    @icon = value
    write_metadata
    start
  end

  def write_metadata
    text = ""
    if @dev_id
      text += "devid=#{@dev_id}\n"
    else
      text += "#devid=myname\n"
    end

    if @dev_title
      text += "devtitle=#{@dev_title}\n"
    else
      text += "#devtitle=My Name\n"
    end

    if @game_id
      text += "gameid=#{@game_id}\n"
    else
      text += "#gameid=gameid\n"
    end

    if @game_title
      text += "gametitle=#{@game_title}\n"
    else
      text += "#gametitle=Game Name\n"
    end

    if @version
      text += "version=#{@version}\n"
    else
      text += "#version=0.1\n"
    end

    if @icon
      text += "icon=metadata/#{@icon}\n"
    else
      text += "#icon=metadata/icon.png\n"
    end

    $gtk.write_file metadata_file_path, text
  end

  def relative_path
    (File.dirname $gtk.binary_path)
  end

  def package_command
    "#{File.join $gtk.get_base_dir, 'dragonruby-publish'}"
  end

  def deploy
    log_info "* Running dragonruby-publish: #{package_command}"
    results = $gtk.exec package_command
    log "#+begin_src"
    log results
    log "#+end_src"
    :success
  end

  def start
    log "================"
    log "* INFO: Starting Itch Wizard."
    @start_at = Kernel.global_tick_count
    steps.each do |m|
      begin
        log_info "Running Itch Wizard Step: ~$wizards.itch.#{m}~"
        result = (send m) || :success if @wizard_status[m][:result] != :success
        @wizard_status[m][:result] = result
        if result != :success
          log_info "Exiting wizard. :#{result}"
          break
        end
      rescue Exception => e
        if e.is_a? WizardException
          $console.log.clear
          $console.archived_log.clear
          log "=" * $console.console_text_width
          e.console_primitives.each do |p|
            $console.add_primitive p
          end
          log "=" * $console.console_text_width
          $console.set_command (e.console_command || "$wizards.itch.start")
        else
          log_error "Step #{m} failed."
          log_error e.to_s
          $console.set_command "$wizards.itch.start"
        end

        break
      end
    end
  end

  def reset
    @dev_id = nil
    @dev_title = nil
    @game_id = nil
    @game_title = nil
    @version = nil
    @icon = nil
    init_wizard_status
  end

  def restart
    reset
    start
  end

  def initialize
    reset
  end

  def init_wizard_status
    @wizard_status = {}

    steps.each do |m|
      @wizard_status[m] = { result: :not_started }
    end

    previous_step = nil
    next_step = nil

    steps.each_cons(2) do |current_step, next_step|
      @wizard_status[current_step][:next_step] = next_step
    end

    steps.reverse.each_cons(2) do |current_step, previous_step|
      @wizard_status[current_step][:previous_step] = previous_step
    end
  end
end