function kvwriteFile($file, $arr)
{
    $contents = kvwrite($arr);
    $fh = fopen($file, 'w');
    fwrite($fh, $contents);
    fclose($fh);
}
function GenerateTheater()
{
    global $theater, $version, $theaterfile, $snippet_path, $snippets, $mod, $mods;
    $data = array();
    $hdr = array();
    //"// Theater generated");
    $ib = $_REQUEST['include_all_theaters'] == 'on';
    //$basedata = array_merge_recursive(ParseTheaterFile($base,$mod,$version,$path,$base_theaters),$basedata);
    //$theater = theater_array_replace_recursive($basedata,$theater);
    if ($ib) {
        $hdr[] = "//\"#base\"\t\t\"mods/{$mod}/{$version}/scripts/theaters/{$theaterfile}.theater\"";
        $data = $theater;
    } else {
        array_unshift($hdr, "\"#base\" \"{$theaterfile}.theater\"");
        $hdr[] = "//\"#base\"\t\t\"mods/{$mod}/{$version}/scripts/theaters/{$theaterfile}.theater\"";
    }
    foreach ($_REQUEST['section'] as $section => $snippet) {
        //var_dump($section,$snippet);
        if (is_array($snippet)) {
        } else {
            if (!strlen($snippet)) {
                echo "not parsing {$section}\n";
                continue;
            }
            $snippet = array($snippet => "on");
        }
        foreach ($snippet as $sname => $sval) {
            $hdr[] = "//\"#base\"\t\t\"snippets/{$section}/{$sname}.theater\"";
            $data = theater_array_replace_recursive(ParseTheaterFile("{$sname}.theater", $mod, $version, "{$snippet_path}/{$section}"), $data);
        }
    }
    foreach ($_REQUEST['mutator'] as $mname => $mdata) {
        if (!strlen($mdata)) {
            continue;
        }
        foreach ($_REQUEST['setting'][$mname] as $section => $settings) {
            foreach ($settings as $key => $val) {
                $hdr[] = "//mutator: {$section}.{$key}: {$val}";
                foreach ($theater[$section] as $iname => $idata) {
                    $data[$section][$iname][$key] = $val;
                }
            }
        }
    }
    $onlythese = array();
    $group_keys = array('weapons' => 'weapon', 'weapon_upgrades' => 'weapon_upgrade', 'player_gear' => 'gear');
    foreach ($_REQUEST['item_groups'] as $gname => $gstatus) {
        if ($gstatus == 'Ignore') {
            continue;
        }
        $hdr[] = "// Change weapon group {$gname} to {$gstatus}";
        $gdata = $snippets['item_groups']['settings'][$gname];
        //		$weapons = (isset($gdata['weapons'])) ? $gdata['weapons'] : array();
        //		$weapon_upgrades = (isset($gdata['weapon_upgrades'])) ? $gdata['weapon_upgrades'] : array();
        //		$player_gear = (isset($gdata['player_gear'])) ? $gdata['player_gear'] : array();
        //		$filters = (isset($gdata['filters'])) ? $gdata['filters'] : array();
        foreach ($theater['player_templates'] as $cname => $cdata) {
            $allowed_items = $gstatus == 'OnlyThese' ? $onlythese : $cdata['allowed_items'];
            foreach ($gdata as $field => $items) {
                if (!isset($theater[$field]) && !isset($data[$field])) {
                    continue;
                }
                foreach ($items as $item) {
                    $match = -1;
                    foreach ($allowed_items as $idx => $pair) {
                        foreach ($pair as $type => $name) {
                            //var_dump($type,$field,$name,$item);
                            if (($type == $field || $type == $group_keys[$field]) && $name == $item) {
                                $match = $idx;
                                break;
                            }
                        }
                    }
                    switch ($gstatus) {
                        case 'Disable':
                            if ($match > -1) {
                                unset($allowed_items[$match]);
                            }
                            break;
                        case 'AllClasses':
                        case 'OnlyThese':
                            if ($match == -1) {
                                $allowed_items[] = array($group_keys[$field] => $item);
                            }
                            break;
                    }
                }
            }
            //var_dump($allowed_items);
            if ($allowed_items != $cdata['allowed_items']) {
                if ($gstatus == 'OnlyThese') {
                    $onlythese = $allowed_items;
                }
                $data['player_templates'][$cname]['allowed_items'] = $allowed_items;
            }
        }
    }
    $kvdata = kvwrite(array('theater' => $data));
    //var_dump($kvdata);
    return implode("\n", $hdr) . "\n" . $kvdata;
}